Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Reverse a Linked List — Part 2

This post is the exten­sion of our ear­lier post Reverse a linked list. Here We have pro­vided the bet­ter recur­sive solu­tion in which we start revers­ing the list from the end.

Objec­tive: Reverse the given linked list.

Input: A Linked List

Out­put: Reversed Linked List

Exam­ple:

Original List :->10->8->6->4->2
Reversed List :->2->4->6->8->10

Approach:

Recur­sion:

  • Tra­verse till the end of list through recursion.
  • Make the last node as head.
  • Now you are at the end of the list and rest of the nodes are stores in a stack
  • Now while com­ing back, each node will pop out from the stack in reverse order
  • take these nodes and start point­ing it to next node com­ing out of stack.
reverse Linked List - Part 2

reverse Linked List — Part 2

Com­plete Code:

Output:
Original List :
->10->8->6->4->2
Reversed List :
->2->4->6->8->10

 

You may also like...