Given pointer to the head node of a linked list, the task is to reverse the linked list.

Examples:

Input : Head of following linked list  
       1->2->3->4->NULL
Output : Linked list should be changed to,
       4->3->2->1->NULL

Input : Head of following linked list  
       1->2->3->4->5->NULL
Output : Linked list should be changed to,
       5->4->3->2->1->NULL

Input : NULL
Output : NULL

Input  : 1->NULL
Output : 1->NULL
[ad type=”banner”]

Iterative Method
Iterate trough the linked list. In loop, change next to prev, prev to current and current to next

Java Programming:

[pastacode lang=”java” manual=”%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20Link%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node*%20next%3B%0A%7D%3B%0A%20%0A%2F*%20Function%20to%20reverse%20the%20linked%20list%20*%2F%0Astatic%20void%20reverse(struct%20node**%20head_ref)%0A%7B%0A%20%20%20%20struct%20node*%20prev%20%20%20%3D%20NULL%3B%0A%20%20%20%20struct%20node*%20current%20%3D%20*head_ref%3B%0A%20%20%20%20struct%20node*%20next%3B%0A%20%20%20%20while%20(current%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20next%20%20%3D%20current-%3Enext%3B%20%20%0A%20%20%20%20%20%20%20%20current-%3Enext%20%3D%20prev%3B%20%20%20%0A%20%20%20%20%20%20%20%20prev%20%3D%20current%3B%0A%20%20%20%20%20%20%20%20current%20%3D%20next%3B%0A%20%20%20%20%7D%0A%20%20%20%20*head_ref%20%3D%20prev%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20push%20a%20node%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%20%20%20%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20print%20linked%20list%20*%2F%0Avoid%20printList(struct%20node%20*head)%0A%7B%0A%20%20%20%20struct%20node%20*temp%20%3D%20head%3B%0A%20%20%20%20while(temp%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%20%22%2C%20temp-%3Edata)%3B%20%20%20%20%0A%20%20%20%20%20%20%20%20temp%20%3D%20temp-%3Enext%3B%20%20%0A%20%20%20%20%7D%0A%7D%20%20%20%20%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Start%20with%20the%20empty%20list%20*%2F%0A%20%20%20%20struct%20node*%20head%20%3D%20NULL%3B%0A%20%20%20%0A%20%20%20%20%20push(%26head%2C%2020)%3B%0A%20%20%20%20%20push(%26head%2C%204)%3B%0A%20%20%20%20%20push(%26head%2C%2015)%3B%20%0A%20%20%20%20%20push(%26head%2C%2085)%3B%20%20%20%20%20%20%0A%20%20%20%20%20%0A%20%20%20%20%20printf(%22Given%20linked%20list%5Cn%22)%3B%0A%20%20%20%20%20printList(head)%3B%20%20%20%20%0A%20%20%20%20%20reverse(%26head)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20printf(%22%5CnReversed%20Linked%20list%20%5Cn%22)%3B%0A%20%20%20%20%20printList(head)%3B%20%20%20%20%0A%20%20%20%20%20getchar()%3B%0A%7D” message=”” highlight=”” provider=”manual”/]
Given linked list
85 15 4 20 
Reversed Linked list 
20 4 15 85

Time Complexity: O(n)
Space Complexity: O(1)

[ad type=”banner”]

Recursive Method:

   1) Divide the list in two parts - first node and rest of the linked list.
   2) Call reverse for the rest of the linked list.
   3) Link rest to first.
   4) Fix head pointer

Write a function to reverse a linked list

C Programming:

[pastacode lang=”c” manual=”void%20recursiveReverse(struct%20node**%20head_ref)%0A%7B%0A%20%20%20%20struct%20node*%20first%3B%0A%20%20%20%20struct%20node*%20rest%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%2F*%20empty%20list%20*%2F%0A%20%20%20%20if%20(*head_ref%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20return%3B%20%20%20%0A%20%0A%20%20%20%20%2F*%20suppose%20first%20%3D%20%7B1%2C%202%2C%203%7D%2C%20rest%20%3D%20%7B2%2C%203%7D%20*%2F%0A%20%20%20%20first%20%3D%20*head_ref%3B%20%20%0A%20%20%20%20rest%20%20%3D%20first-%3Enext%3B%0A%20%0A%20%20%20%20%2F*%20List%20has%20only%20one%20node%20*%2F%0A%20%20%20%20if%20(rest%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20return%3B%20%20%20%0A%20%0A%20%20%20%20%2F*%20reverse%20the%20rest%20list%20and%20put%20the%20first%20element%20at%20the%20end%20*%2F%0A%20%20%20%20recursiveReverse(%26rest)%3B%0A%20%20%20%20first-%3Enext-%3Enext%20%20%3D%20first%3B%20%20%0A%20%20%20%20%20%0A%20%20%20%20%2F*%20tricky%20step%20–%20see%20the%20diagram%20*%2F%0A%20%20%20%20first-%3Enext%20%20%3D%20NULL%3B%20%20%20%20%20%20%20%20%20%20%0A%20%0A%20%20%20%20%2F*%20fix%20the%20head%20pointer%20*%2F%0A%20%20%20%20*head_ref%20%3D%20rest%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%7D” message=”” highlight=”” provider=”manual”/]

Time Complexity: O(n)
Space Complexity: O(1)

[ad type=”banner”]

Java Programming:

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20for%20reversing%20the%20Linked%20list%0A%20%0Aclass%20LinkedList%20%7B%0A%20%0A%20%20%20%20static%20Node%20head%3B%0A%20%0A%20%20%20%20static%20class%20Node%20%7B%0A%20%0A%20%20%20%20%20%20%20%20int%20data%3B%0A%20%20%20%20%20%20%20%20Node%20next%3B%0A%20%0A%20%20%20%20%20%20%20%20Node(int%20d)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20next%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20A%20simple%20and%20tail%20recursive%20function%20to%20reverse%0A%20%20%20%20%2F%2F%20a%20linked%20list.%20%20prev%20is%20passed%20as%20NULL%20initially.%0A%20%20%20%20Node%20reverseUtil(Node%20curr%2C%20Node%20prev)%20%7B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20If%20last%20node%20mark%20it%20head*%2F%0A%20%20%20%20%20%20%20%20if%20(curr.next%20%3D%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20head%20%3D%20curr%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Update%20next%20to%20prev%20node%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20curr.next%20%3D%20prev%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20null%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Save%20curr-%3Enext%20node%20for%20recursive%20call%20*%2F%0A%20%20%20%20%20%20%20%20Node%20next1%20%3D%20curr.next%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20and%20update%20next%20..*%2F%0A%20%20%20%20%20%20%20%20curr.next%20%3D%20prev%3B%0A%20%0A%20%20%20%20%20%20%20%20reverseUtil(next1%2C%20curr)%3B%0A%20%20%20%20%20%20%20%20return%20head%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20prints%20content%20of%20double%20linked%20list%0A%20%20%20%20void%20printList(Node%20node)%20%7B%0A%20%20%20%20%20%20%20%20while%20(node%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(node.data%20%2B%20%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20node%20%3D%20node.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20LinkedList%20list%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20list.head%20%3D%20new%20Node(1)%3B%0A%20%20%20%20%20%20%20%20list.head.next%20%3D%20new%20Node(2)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next%20%3D%20new%20Node(3)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next.next%20%3D%20new%20Node(4)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next.next.next%20%3D%20new%20Node(5)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next.next.next.next%20%3D%20new%20Node(6)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next.next.next.next.next%20%3D%20new%20Node(7)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next.next.next.next.next.next%20%3D%20new%20Node(8)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Original%20Linked%20list%20%22)%3B%0A%20%20%20%20%20%20%20%20list.printList(head)%3B%0A%20%20%20%20%20%20%20%20Node%20res%20%3D%20list.reverseUtil(head%2C%20null)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Reversed%20linked%20list%20%22)%3B%0A%20%20%20%20%20%20%20%20list.printList(res)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%20%0A%2F%2F%20This%20code%20has%20been%20contributed%20by%20Mayank%20Jaiswal” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Output:

Given linked list
1 2 3 4 5 6 7 8

Reversed linked list
8 7 6 5 4 3 2 1