Linked List

How will you reverse a singly linked list?

Questions by suchana datta

Showing Answers 1 - 30 of 30 Answers

Swap the pointers (references) of the head and tail nodes win the list.

node swap = null;
swap = head;
head = tail;
tail = swap;

The next traversal through the list should be reversed.
      

The prev. answer seemed pretty much on track, but wouldn't pass as an interview answer on the white board. Here's my equiv. Double check for stupid errors.

Code
  1. pList  reverse(pList head){

  2.     pList reversed, swap = null;

  3.     while (head != null){

  4.         swap = head;

  5.         head = head.next;

  6.         swap.next = reversed;

  7.         reversed = swap;

  8.     }

  9.     return reversed;

  10. }

  Was this answer useful?  Yes

nishkarshs

  • Dec 30th, 2010
 

How to think: Make a 'temp' node similar to any other node. This has a 'next' element. So, all we need now is the 'prev' element. Hence, we create a pointer variable prev.

Algo:

Code
  1. prev = NULL

  2. node = head_node // the starting node of linked list

  3.  

  4. while (node.next != NULL) {

  5.   temp = node

  6.   node.next = prev

  7.   prev = temp

  8.   node = temp.next

  9. }


// done!

Chirag

  • Oct 11th, 2011
 

Here is the code

Code
  1. #include <stdio.h>

  2. typedef struct node mnode;

  3. mnode* reverse_ll(mnode* head)

  4. {

  5.         if(head==NULL)

  6.                 return;

  7.         mnode *temp1,*temp2,*temp3;

  8.        

  9.        

  10.         temp2=head->next;

  11.         head->next=NULL;

  12.         while(temp2!=NULL)

  13.         {

  14.                 temp3=temp2->next;

  15.                 temp2->next=head;

  16.                 head=temp2;

  17.                 temp2=temp3;

  18.         }

  19.         return head;

  20.        

  21. }

  Was this answer useful?  Yes

Shikhar Singhal

  • Jun 10th, 2013
 

Code
  1. list* reverse( list *head)

  2. {

  3. list *a=head,*Prev=NULL, *Next= NULL;

  4.  

  5. while (a.next!=NULL)

  6. {

  7.      Next=a.next;

  8.      a.next=Prev;

  9.      Prev= a;

  10.      a=Next;

  11. }

  12. return *a;

  13. }

  Was this answer useful?  Yes

usan007

  • Aug 8th, 2014
 

Code with comments for clarity

Code
  1.         public Node reverse(Node head) {               

  2.                 // traverse the list

  3.                 // make each node point to its previous node

  4.                 Node previous = null, current = head, next = null;             

  5.                 while (current != null) {

  6.                         next = current.next;            // store next nodes pointer

  7.                         current.next = previous;        // make current node point to previous node

  8.                         previous = current;             // adjust previous for next iteration   (move previous to current)

  9.                         current = next;                 // adjust current for next iteration    (move current to next)

  10.                 }

  11.                 return previous;

  12.         }              

  Was this answer useful?  Yes

Eric Nantel

  • Apr 22nd, 2015
 

Use a stack so when you pop you get the reverse order !!

Code
  1. auto it = list->begin();

  2. auto stack;

  3.  

  4. while(it != list->end())

  5. {

  6.       stack.push(it);

  7.       it = it->next;

  8. }

  9. list->clear();

  10. while( !stack.empty())

  11. {

  12.    list->insert(stack.pop());

  13. }

  Was this answer useful?  Yes

Eric Nantel

  • Apr 22nd, 2015
 

No ,it is said that this is a SINGLE-linked list , if you try head->next it points to head !

  Was this answer useful?  Yes

Asad

  • Jun 7th, 2015
 

Code
  1. public void reverse(Node node) {

  2.     if (node.next == null) {

  3.          head = node;

  4.          return;

  5.     }

  6.     reverse(node.next);

  7.     node.next.next = node;

  8.     node.next = null;

  9. }

  Was this answer useful?  Yes

Code2Live

  • Jun 9th, 2015
 

Code
  1. reverse(Node root)

  2. {

  3.   Node x = root;

  4.   if (x != null)

  5.   {

  6.     Stack<Node> nodes = new Stack<Node>();

  7.     while(x != null)

  8.     {

  9.       nodes.push(x);

  10.       x = x.next;

  11.     }

  12.    

  13.     root = nodes.pop();

  14.     x = root;

  15.     while(!nodes.isEmpty())

  16.    {

  17.       x.next = nodes.pop();

  18.       x = x.next;

  19.     }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions