Reverse a Linked List with recursion
  • node
  • recursion
  • reverse
  • stackqueuelinkedlist
  • linkedlist
  •   
  • Posted: 7 years ago
  • Edit
  • answers (1)
  • views (3506)

Write a function to reverse a Linked List using recursion.


Posted Answers



public ListNode Reverse(ListNode list){

/* If the list is empty */
if(list == null)
return null;

/* If the list has only one node */
if(list.next == null)
return list;

ListNode secondElement = list.next;
list.next = null;
ListNode reverseRest = Reverse(secondElement);

/* Join the two lists */
secondElement.next = list;
return reverseRest;
}

You need to Sign In to post your solution.