Reverse a sublist of a linked list | Practice | GeeksforGeeks

Given a linked list and positions m and n. Reverse the linked list from position m to n.
Example 1:
Input :
N = 10
Linked List = 1->7->5->3->9->8->10
                      ->2->2->5->NULL
m = 1, n = 8
Output : 2 10 8 9 3 5 7 1 2 5 
Explanation :
The nodes from position 1 to 8 
are reversed, resulting in 
2 10 8 9 3 5 7 1 2 5.


Example 2:
Input:
N = 6
Linked List = 1->2->3->4->5->6->NULL
m = 2, n = 4
Output: 1 4 3 2 5 6
Explanation:
Nodes from position 2 to 4 
are reversed resulting in
1 4 3 2 5 6.
0
nareshshiva93171 day ago
Can someone please tell me what is the problem in this code?
Node* reverseBetween(Node* head, int m, int n)   {       if(head == NULL || m == n || head->next == NULL){           return head;       }       //m and n is given        Node* ptr = head;       Node* newll = new Node(-1);       Node* newlhead = NULL;       int cnt = 1;       while(ptr!=NULL){           if(cnt == m){               while(cnt<=n){                   if(newlhead == NULL){                       newlhead = ptr;                   }                   newll->next = ptr;                   newll = newll->next;                   ptr = ptr->next;                   cnt++;               }               break;           }           cnt++;           ptr = ptr->next;       }       newll->next = NULL;       Node* curr = newlhead;       Node* prev = NULL;       Node* nxt = NULL;       while(curr!=NULL){           nxt = curr->next;           curr->next = prev;           prev = curr;           curr = nxt;       }       curr = prev;       //reversed       int start = 1;       Node* n1 = new Node(-1);       Node* n1h = NULL;       Node* n2 = new Node(-1);       Node* n2h = NULL;       int c1 = 0;       int c2 = 0;       while(head!=NULL){           if(start<m){               if(n1h == NULL){                   n1h = head;               }               c1 = c1 + 1;               n1->next = head;               n1 = n1->next;           }           else if(start>n){               if(n2h == NULL){                   n2h = head;               }               c2 = c2 + 1;               n2->next = head;               n2 = n2->next;           }           start = start + 1;           head = head->next;       }       if(c1 == 0 && c2 > 0){           newlhead->next = n2h;           n2->next = NULL;           return curr;       }       if(c1 >0 && c2 > 0){           n1->next = curr;           newlhead->next = n2h;           n2->next = NULL;           return n1h;       }       if(c1 > 0 && c2 == 0){           n1->next = curr;           newlhead->next = NULL;           return n1h;       }       if(c1 == 0 && c2 == 0){           newlhead->next = NULL;           return curr;       }       //code here   }
 
0
hokage4u2 days ago
// If it helps

public static Node reverseBetween(Node head, int m, int n)
    {
        //code here
        int count = 1;
        
        Node leftTail = null;
        Node rightHead = null;
        
        Node current = head;
        
        while(count < n)
        {
            if(count == m-1)
            leftTail = current;
            
            current = current.next;
            count++;
        }
        
        rightHead = current.next;
        current.next = null;
        
        Node revHead = leftTail == null ? reverse(head):reverse(leftTail.next);
        
        
        Node temp = revHead;
        while(temp.next != null)
        temp = temp.next;
        
        temp.next = rightHead;
        
        Node newHead = leftTail == null ? revHead : head;
        
        if(leftTail != null)
        leftTail.next = revHead;
        
        return newHead;
        
    }
    private static Node reverse(Node head)
    {
        if(head == null || head.next == null)
        return head;
        
        Node newHead = reverse(head.next);
        
        Node tail = head.next;
        tail.next = head;
        head.next = null;
        
        return newHead;
    }
0
shakshamkaushik12 days ago
 
Time -→1.77
   public static Node reverseBetween(Node head, int m, int n)   {       Node rev = new Node(0);       Node rev1 = rev;       Node temp = head;       int count = 1;       while (temp!=null){           if(count>=m && count<=n){               rev.next = new Node(temp.data);               rev = rev.next;           }           count++;           temp = temp.next;       }       Node curr = rev1.next;       Node prev = null;       while(curr!=null){           Node t = curr.next;           curr.next = prev;           prev = curr;           curr = t;       }       Node tn = head;       int count1 = 1;       while(prev!=null && tn!=null){           if(count1>=m && count1<=n){               tn.data = prev.data;               prev = prev.next;                          }           tn = tn.next;           count1++;       }       return head;              }        
0
vishalsavade1 week ago
//This code is contributed by Vishal Savade
//Easy to understand C++ in 0.2/1.31
//Any update or optimization write it into comment
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;
    }
    Node* reverseBetween(Node* head, int m, int n)
    {
        //code here
        if(m == n) return head;
        int cnt = 0;
        Node *h1 = head, *p1 = head;
        
        //Finding first index 
        while(cnt+1 != m){
            p1 = h1;
            h1 = h1->next;
            cnt++;
        }
        
        Node *h2 = h1;
        cnt = 0;
        //Finding second index 
        while(cnt != abs(n-m)){
            h2 = h2->next;
            cnt++;
        }
        //Store right part of the list
        Node *right_part = h2->next;
        h2->next = NULL;
        
        //Reverse the sublist
        Node *reversed = reverse(h1);
        
        if(m == 1) head = reversed;
        else{
            p1->next = reversed;
        }
        //Attach the right part to end of the list back
        Node *t = reversed;
        while(t->next!=NULL) t = t->next;
        t->next = right_part;
        
        return head;
    }
+2
shubhamkhavare2 weeks ago
Very Easy Java Code:
      Node rev = new Node(0);       Node rev1 = rev;       Node temp = head;       int count = 1;       while(temp != null)       {           if(count >= m && count <= n)           {               rev.next = new Node(temp.data);               rev = rev.next;           }           count++;           temp = temp.next;       }       Node curr = rev1.next;       Node prev = null;       while(curr != null)       {           Node temp1 = curr.next;           curr.next = prev;           prev = curr;           curr = temp1;       }       Node tempNode = head;       int count1 = 1;       while(prev != null && tempNode != null)       {           if(count1 >= m && count1 <= n)           {               tempNode.data = prev.data;               prev = prev.next;           }           tempNode = tempNode.next;           count1++;       }       return head;
+1
lakshta192 months ago
 Node* reverseBetween(Node* head, int m, int n)   {       Node*p=head,*q=head, *b,*f;   if(m==1){           while(n-1){           q=q->next;           f=q;           n--;       }       f=q->next;       q->next=NULL;       Node *r=NULL, *t=NULL, *s=p;       while(p){           r=t;           t=p;           p=p->next;           t->next=r;       }       s->next=f;       head=q;       return head;       }   else {       while(m-1){           b=p;           p=p->next;           m--;       }       while(n-1){           q=q->next;           f=q;           n--;       }       f=q->next;       b->next=NULL;       q->next=NULL;       Node *r=NULL, *t=NULL, *s=p;       while(p){           r=t;           t=p;           p=p->next;           t->next=r;       }       b->next=t;       s->next=f;       return head;   }}
0
hanumanmanyam8372 months ago
public static Node reverseBetween(Node head, int m, int n)
    {
        if(head == null || m == n) return head;
        int count=0;
        Node curr=head;
        Node prevFirst=null;
        while(curr!=null)
        {
            count++;
            if(count==m)
            {
                Node first=curr;
                Node prev=null;
                while(first!=null && count<=n)
                {
                    Node temp=first.next;
                    first.next=prev;
                    prev=first;
                    first=temp;
                    count++;
                }
                if(m==1)
                {
                    head=prev;
                }
                else
                {
                    prevFirst.next=prev;
                }
                curr.next=first;
                break;
            }
            prevFirst=curr;
            curr=curr.next;
        }
        return head;
        
    }
+1
ciph3rcodes2 months ago
#JAVA EASY SOLUTION 
 
class Solution{   public static Node reverseBetween(Node head, int m, int n)   {     Node temp=head;      Node t=head;      int c=0;      ArrayList<Integer> arrli=new ArrayList<>();            while(t!=null){          arrli.add(t.data);          t=t.next;          c++;      }      ArrayList<Integer> arrli1= new ArrayList<>();      for(int i=m-1;i<n;i++){          arrli1.add(arrli.get(i));      }      int k=0;      Collections.reverse(arrli1);      for(int i=0;i<c;i++){          if(i+1>=m && i+1<=n){              int a=arrli1.get(k);              k++;              temp.data=a;              temp=temp.next;          }          else{              int b=arrli.get(i);              temp.data=b;              temp=temp.next;          }      }      return head;   }}
0
mridulbhaskarabc2 months ago
 
class Solution:
    def reverseBetween(self, head, m, n):
        #code here
        node_ = head
        node_temp = head
        count = n-m+1
        list_ = [0]*count
        for i in range(m-1):
            node_ = node_.next
            node_temp = node_temp.next
        for i in range(count):
            list_[count-i-1] = node_.data
            node_ = node_.next
        for i in range(count):
            node_temp.data = list_[i]
            node_temp = node_temp.next
        return head
#Contributed By: Mridul Bhaskar
 
 
0
chessnoobdj2 months ago
C++
Node* reverseBetween(Node* head, int m, int n)
    {
        Node *ptr = head, *prev = head, *tmp1 = NULL, *tmp2 = head;
        while(ptr){
            if(m == 1){
                while(ptr && n>0){
                    Node *tmp = ptr->next;
                    ptr->next = prev;
                    prev = ptr;
                    ptr = tmp;
                    n -= 1;
                }
                if(tmp1 == NULL){
                    tmp1 = prev;
                    head = prev;
                }
                else
                    tmp1->next = prev;
                tmp2->next = ptr;
                break;
            }
            m -= 1;
            n -= 1;
            tmp1 = ptr;
            tmp2 = ptr->next;
            ptr = ptr->next;
        }
        return head;
    }
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.