Reverse a linked list | Practice | GeeksforGeeks

Given a linked list of N nodes. The task is to reverse this list.
Example 1:
Input:
LinkedList: 1->2->3->4->5->6
Output: 6 5 4 3 2 1
Explanation: After reversing the list, 
elements are 6->5->4->3->2->1.

Example 2:
Input:
LinkedList: 2->7->8->9->10
Output: 10 9 8 7 2
Explanation: After reversing the list,
elements are 10->9->8->7->2.
Your Task:
The task is to complete the function reverseList() with head reference as the only argument and should return new head after reversing the list.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 104
 
0
itsmehrithikroshanin 12 hours
  struct Node* reverseList(struct Node *head)   {       // code here       // return head of reversed list       struct Node *final=NULL;       while(head)       {           struct Node *temp=head->next;           head->next=final;           final=head;           head=temp;       }       return final;   }
0
bhavdeepkaushal392in 4 hours
//recursive function
struct Node* reverseList(struct Node *head)   {       if(head==NULL || head->next==NULL){           return head;       }              struct Node * ans = reverseList(head->next);       head->next->next=head;       head->next=NULL;                     return ans;   }
0
bhavdeepkaushal392in 4 hours
struct Node* reverseList(struct Node *head)   {       struct Node* curr = head;       struct Node* prev = NULL;       struct Node* nxtptr;       while(curr!=NULL){           nxtptr = curr->next;           curr->next=prev;           prev=curr;           curr=nxtptr;       }              return prev;   }
0
deepakkamble5653 days ago
class Solution{   public:   //Function to reverse a linked list.   struct Node* reverseList(struct Node *head)   {       Node * prev=NULL;       Node * curr=head;       Node * next=head;   while(next!=NULL){   next=next->next;   curr->next=prev;   prev=curr;   curr=next;          }      head=prev;}   };
+1
biswassuraj2053 days ago
JAVA SOLUTION | EASY UNDERDATING
class Solution
{
    //Function to reverse a linked list.
    Node reverseList(Node head)
    {
        if(head == null || head.next == null) return head;
        
        Node curr = head, prev = null;
        
        while(curr != null){
            Node forw = curr.next;
            
            curr.next = prev;
            
            prev = curr;
            curr = forw;
        }
        
        return prev;
    }
}

 
0
prabhukumarsawraj4 days ago
using sliding pointer :
means all pointer slide
p= Initializing First node
q= NULL before p and following p
r= NULL before q and following q
1st slide
	  2 → 4 → 6 → 8 →null
r  q  p
 
2st slide
	  2 → 4 → 6 → 8 →null
   r  q   p

	  2 → 4 → 6 → 8 →null
   			  r   q   p
class Solution
{
    public:
    //Function to reverse a linked list.
    struct Node* reverseList(struct Node *p)
    {
        // code here
        // return head of reversed list
       struct Node *q=NULL,*r=NULL;

    while(p!=NULL)
     {
        r=q;
        q=p;
        p=p->next;
        q->next=r;
     }  
 return q;
    }
    
};
0
pranjalsinghkatiyar6 days ago
SIMPLE to Understand Approach (C++)
struct Node* reverseList(struct Node *head)   {       int ctr=1;       struct Node *temp=head;       struct Node *t,*t1;       while(temp!=NULL)       {           t1=temp->next;           if(ctr==1)           {               temp->next=NULL;           }           else           {               temp->next=t;           }           t=temp;           temp=t1;           ctr++;       }       head=t;       return head;   } 
0
mannukhurana103971 week ago
Java
 
 //Function to reverse a linked list.
    Node reverseList(Node head)
    {
        // time = O(n) : 3.38
        // space = O(1)
        Node rev=null;
        Node temp = head;
        while(temp!=null){
            Node newNode = new Node(temp.data);
            if(rev==null){
                rev = newNode;
            }
            else{
                newNode.next = rev;
                rev = newNode;
            }
            temp=temp.next;
        }
        return rev;
    }
0
hokage4u1 week ago
RECURSIVE APPROACH - 
If we already have the  next remaining linked List reversed except for the head, what do we need to do next?? The answer is simple, we just need to traverse the reversed LinkedList which we already have till we get the tail.Once we get tail we need to link the tail to head and make head.next = nullWhy do we need to make head.next = null ?? because a linked list is complete if tail of the LL points to null, which after reversing the LL the head now becomes tail, so we need to make head.next = null
If we already have the  next remaining linked List reversed except for the head, what do we need to do next?? The answer is simple, we just need to traverse the reversed LinkedList which we already have till we get the tail.
Once we get tail we need to link the tail to head and make head.next = null
Why do we need to make head.next = null ?? because a linked list is complete if tail of the LL points to null, which after reversing the LL the head now becomes tail, so we need to make head.next = null
---------------SPOILER ALERT : CODE AHEAD :-------------------
Node reverseList(Node head)
    {
        // code here
        if(head == null || head.next == null)
        return head;
        
        Node newHead = reverseList(head.next);
        
        Node tail = head.next;
        tail.next = head;
        
        head.next = null;
        
        return newHead;
    }
0
adarsh471111 week ago
Java Solution
class Solution
{
    //Function to reverse a linked list.
    Node reverseList(Node head)
    {
        if(head==null)
        return head;
        
        if(head.next==null)
        return head;
        
        Node temp=head.next;
        Node newH=reverseList(temp);
        
        temp.next=head;
        head.next=null;
        
        return newH;
    }
}
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.