#100 days of LeetCode

Day 1 of 100

100daysof.codes

Solution:

  1. Swapping Nodes in a Linked List

  2. Reorder List

    // 1-> Swapping Nodes in the Linked List
    ListNode* swapNodes(ListNode* head, int k) {
      vector<int> v;
      ListNode* temp = head;
      while(temp != nullptr){
        v.push_back(temp -> val);
        temp = temp -> next;
      }
      swap(v[k-1], v[v.size()-1-k+1]);
      temp = head;
      for(auto i =0 ;i<v.size(); i++){
        temp -> val = v[i];
        temp = temp -> next;
      }
      return head;
    }

// 2-> Reorder List
    void reorderList(ListNode* head) {
        vector<int>v;
        ListNode* temp = head;
        while(temp != NULL){
            v.push_back(temp -> val);
            temp = temp -> next;
        }
        int s = 0; int e = v.size()-1;
        temp = head;
        while(temp!= nullptr){
            temp -> val = v[s];
            if(temp -> next != nullptr){
                temp = temp -> next;
                temp -> val = v[e];
            }
            temp = temp -> next;
            s++;e--;
        }


    }