Reorder List | Practice | GeeksforGeeks

Given a singly linked list: A0→A1→...→An-1→An, reorder it to: A0→An→A1→An-1→A2→An-2→...
For example: Given 1->2->3->4->5 its reorder is 1->5->2->4->3.
Note: It is recommended do this in-place without altering the nodes' values.
Example 1:
Input:
LinkedList: 1->2->3
Output: 1 3 2

Example 2:
Input:
LinkedList: 1->7->3->4
Output: 1 4 7 3.
Your Task:
The task is to complete the function reorderList() which should reorder the list as required. The reorder list is automatically printed by the driver's code.
Note: Try to solve without using any auxilliary space.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 104
0
vishalsavade5 days ago
//This code is contributed by Vishal Savade
//Find mid previous to get list 1
Node *mid_prev(Node *head){
    Node *slow = head, *fast = head, *pr = head;
    while(fast!=NULL && fast->next != NULL){
        pr = slow;
        slow = slow->next;
        fast = fast->next->next;
    }
    return pr;
}
//Reverse second half of the list
Node *reverse(Node *root){
    Node *curr = root, *prev = NULL, *succ;
    while(curr!=NULL){
        succ = curr->next;
        curr->next = prev;
        
        prev = curr;
        curr = succ;
    }
    return prev;
}
//Merge both l1 and l2
void *merge(Node *l1, Node *l2){
    
    while(l1!=NULL){
        Node *l1_nxt = l1->next;
        Node *l2_nxt = l2->next;
    
        l1->next = l2;
        if(l1_nxt == NULL)
            break;
        l2->next = l1_nxt;
        l1 = l1_nxt;
        l2 = l2_nxt;
    }
}
void reorderList(Node* head) {
    // Your code here
    if(head == NULL or head->next == NULL)
        return;
    //List 1 l1
    Node *l1 = head;
    //List 2 l2 second half
    Node *mp = mid_prev(head);
    Node *l2 = reverse(mp->next);
    mp->next = NULL;
    //Merge both the list
    merge(l1, l2);
}
0
tthakare735 days ago
//java solution 
//hint -> get mid and rev
class gfg {
    Node getMiddal(Node root){
        Node fast = root.next, slow = root;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    
    Node rev(Node root){
        Node pre = null, curr = root, next = root.next;
        while(curr != null){
            curr.next = pre;
            pre = curr;
            curr = next;
            if(next != null) next = next.next;
        }
        return pre;
    }
    
    Node reorderlist(Node head) {
        // Your code here
        if(head == null || head.next == null) return head;
        
        Node mid = getMiddal(head);
        Node temp = rev(mid.next);
        mid.next = null;
        Node result = null, dummy = null;
        while(head != null && temp != null){
            Node t1 = head;
            Node t2 = temp;
            
            head = head.next;
            temp = temp.next;
        
            t1.next = t2;
            
            if(result == null){
                result = t1;
            } else {
                dummy.next = t1;
            }
            dummy = t2;
        }
        // System.out.println(head.data+" "+temp);
        if(head != null){
            dummy.next = head;
        } else if(temp != null){
            dummy.next = temp;
        }
        return result;
    }
}
0
mayank2000j3 weeks ago
Node* reverse(Node *node){
    Node* prev=NULL;
    Node* curr=node;
    Node* nxt=node;
    while(curr!=NULL){
        nxt=curr->next;
        curr->next=prev;
        prev=curr;
        curr=nxt;
    }
    return prev;
}

void merge(Node* l1,Node* l2){
  while(l1!=NULL){
      Node* l1_next = l1->next;
      Node* l2_next = l2->next;
      l1->next=l2;
      if(l1_next==NULL)
        break;
      l2->next=l1_next;
      l1=l1_next;
      l2=l2_next;
  }  
}

void reorderList(Node* head) {
    // Your code here
    if(head==NULL or head->next==NULL)
        return;
    Node* slow=head;
    Node* prev=NULL;
    Node* fast=head;
    Node* l1=head;
    while(fast!=NULL and fast->next!=NULL){
        prev=slow;
        slow=slow->next;
        fast=(fast->next)->next;
    }
    prev->next=NULL;
    Node *l2=reverse(slow);
    merge(l1,l2);
}
0
akashmr10961 month ago
Node reorderlist(Node head) {
        if (head == null || head.next == null) {
            return head;
        }
        
        Deque<Node> stack = new LinkedList<>();
        Node trav = head;
        int total = 0;
        while(trav != null) {
            stack.addLast(trav);
            trav = trav.next;
            total++;
        }
        
        trav = head;
        for(int i=0; i<total/2; i++) {
            Node last = stack.removeLast();
            Node temp = trav.next;
            trav.next = last;
            last.next = temp;
            trav = temp;
        }
        
        trav.next = null;
        return head;
    }
+1
mukeshkumarmandal6061 month ago
Simple java solution 
Node reorderlist(Node head) {       Node temp=head;      ArrayList<Integer> list=new ArrayList<Integer>();      while(temp!=null)      {          list.add(temp.data);          temp=temp.next;      }      ArrayList<Integer> List=new ArrayList<Integer>();      int i=0;      int j=list.size()-1;      while(i<=j)      {          List.add(list.get(i));          List.add(list.get(j));          i++;          j--;      }      Node dummy=new Node(-1);      Node curr=dummy;      int size=List.size()-1;      if(list.size()%2==0)      {         size=List.size();      }           for(int k=0;k<size;k++)      {          Node forward=new Node(List.get(k));          dummy.next=forward;          dummy=dummy.next;      }        return curr.next;       // Your code here   }
0
mashhadihossain1 month ago
SIMPLIFIED JAVA SOLUTION (2.3/4.7 SEC)
class gfg {   Node reorderlist(Node head) {       Node temp=head;       ArrayList<Integer> list=new ArrayList<Integer>();       while(temp!=null)       {           list.add(temp.data);           temp=temp.next;       }       ArrayList<Integer> List=new ArrayList<Integer>();       int i=0;       int j=list.size()-1;       while(i<=j)       {           List.add(list.get(i));           List.add(list.get(j));           i++;           j--;       }       Node dummy=new Node(-1);       Node curr=dummy;       int size=List.size()-1;       if(list.size()%2==0)       {          size=List.size();       }             for(int k=0;k<size;k++)       {           Node forward=new Node(List.get(k));           dummy.next=forward;           dummy=dummy.next;       }         return curr.next;          }} 
+1
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION  (2.1/4.7 SEC)
class gfg {   Node reorderlist(Node head) {       Node temp=head;       ArrayList<Integer> list=new ArrayList<Integer>();       while(temp!=null)       {           list.add(temp.data);           temp=temp.next;       }       ArrayList<Integer> List=new ArrayList<Integer>();       int i=0;       int j=list.size()-1;       while(i<=j)       {           List.add(list.get(i));           List.add(list.get(j));           i++;           j--;       }       Node dummy=new Node(-1);       Node curr=dummy;       if(list.size()%2==0)       {           for(int k=0;k<List.size();k++)         {           Node forward=new Node(List.get(k));           dummy.next=forward;           dummy=dummy.next;         }         return curr.next;       }       else       {         for(int k=0;k<List.size()-1;k++)         {           Node forward=new Node(List.get(k));           dummy.next=forward;           dummy=dummy.next;          }         return curr.next;       }   }} 
0
aakkaa1 month ago
class gfg {   Node reverse(Node head){       Node cur = null, temp;       while(head != null){           temp = head.next;           head.next = cur;           cur = head;           head = temp;       }       return cur;   }   Node reorderlist(Node head) {       // Your code here       Node slow = head, fast = head;       while(fast != null && fast.next != null){           slow = slow.next;           fast = fast.next.next;       }       Node rev = reverse(slow);       Node ans = new Node(0);       Node prev = ans, cur1 = head, cur2 = rev;       int i = 0;              while(cur1 != slow || cur2 != null){
           if(i == 0 && cur1 != slow){               Node temp = new Node(cur1.data);               cur1 = cur1.next;               prev.next = temp;               prev = temp;           }           else if(i == 1 && cur2 != null){               Node temp = new Node(cur2.data);               cur2 = cur2.next;               prev.next = temp;               prev = temp;           }                      if(i==0)               i=1;           else               i=0;       }       prev.next = null;       return ans.next;   }}
0
himanshukug19cs2 months ago
java solution
 
  // Your code here      ArrayList<Integer> al = new ArrayList<>();      while(head!=null){          al.add(head.data);          head=head.next;      }      Node ans = new Node(al.get(0));      Node node=ans;      if(al.size()>1){      node.next=new Node(al.get(al.size()-1));      node=node.next;      int i=1;      int j=al.size()-2;      while(i<=j){          if(i==j){               node.next=new Node(al.get(i));      node=node.next;          }          else{      node.next=new Node(al.get(i));      node=node.next;        node.next=new Node(al.get(j));      node=node.next;          }      i++;      j--;      }      }      return ans;
0
hanumanmanyam8372 months ago
Solution in JAVA
class gfg {
    Node reorderlist(Node head) {
        // Your code here
        if(head.next==null)
        {
            return head;
        }
        gfg s=new gfg();
        Node slow=head;
        Node fast=head;
        while(fast.next!=null && fast.next.next!=null)
        {
            slow=slow.next;
            fast=fast.next.next;
        }
        Node mid=slow;
        Node mid1=slow.next;
        Node newHead=s.reverse(mid1);
        mid.next=null;
        Node curr=head;
        while(curr!= null && newHead!=null)
        {
            Node temp1=newHead.next;
            Node temp=curr.next;
            curr.next=newHead;
            newHead.next=temp;
            curr=temp;
            newHead=temp1;
        }
        return head;
        
    }
    Node reverse(Node head)
    {
        Node curr=head;
        Node prev=null;
        while(curr!=null)
        {
            Node temp=curr.next;
            curr.next=prev;
            prev=curr;
            curr=temp;
        }
        return prev;
    }
}

We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.