Write a function to reverse a linked list

Write a function to reverse a linked list

Solutions:

  • One solution could be take all element reverse using array until and replace with node value
  • Second could be, move one step and set node next point to previous node

Algorithm:

  • Set previous = null
  • Iterate till we reached end of list
    • temp = node.next
    • node.next = previous
    • previous = node
    • node = temp

Latest Source Code:
Github: ReverseList.java


Output:

1 2 3 4 
4 3 2 1 
1 2 
2 1 
Author: Hrishikesh Mishra