Last Updated: 22 Oct, 2020

Reverse Linked List

Moderate
Asked in companies
IBMQuikrMicrosoft

Problem statement

Given a singly linked list of integers. Your task is to return the head of the reversed linked list.

For example:
The given linked list is 1 -> 2 -> 3 -> 4-> NULL. Then the reverse linked list is 4 -> 3 -> 2 -> 1 -> NULL and the head of the reversed linked list will be 4.
Follow Up :
Can you solve this problem in O(N) time and O(1) space complexity?
Input Format :
The first line of input contains an integer 'T' representing the number of test cases or queries to be processed. Then the test case follows.

The only line of each test case contains the elements of the singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
Output Format :
For each test case, print the given linked list in reverse order in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 5
0 <= L <= 10^5
1 <= data <= 10^9 and data != -1

Time Limit: 1 sec

Approaches

01 Approach

The brute force approach is to use recursion. First, we reach the end of the Linked List recursively and at last node, we return the last node, which becomes the new head of the partially reversed Linked List. While coming back from each recursion call we add the current node in the current recursion call to the last node of the partially reversed Linked List and assign the current node to null.

 

Steps:

 

  • We divide the linked list of N nodes into two parts. i.e head and rest of the Linked List with (N-1) nodes.
  • Now recursively reverse the (N-1) nodes of Linked List and return the head of this part i.e reverseHead. After the reversal, the next node of the head will be the last node of the reversed Linked List and the head next will be pointing to this node.
  • But for the complete reversal of the Linked List, the head should be the last node. So, we first find the last node of this reverseHead: Linked List then we do the following:
    • head.next = NULL
    • temp.next = head, where the temp is the last node of the reversed linked list.
  • At last, return the head pointer of the reversed Linked List i.e. return reverseHead.

02 Approach

  • Iterate through all nodes of the Linked list and push it to stack.
  • Then update the head pointer to the top node of the stack.
  • Then start popping each node from the stack and then assign the popped node next pointer to the top element of the stack.
  • At the last update the next pointer of the last node in the stack to null.
  • Now, return the head

03 Approach

We can improve our time complexity of finding the last node of partial reverse Linked List by the current node of each recursion call. If we look closer than the current node next is actually the last node of the reverse Linked List that can be found in constant time.

 

Algorithm:

 

  • We divide the linked list of N nodes into two parts. i.e head and rest of the Linked List with (N-1) nodes.
  • Now recursively reverse the (N-1) nodes of Linked List and return the head of this part i.e rest. After the reversal, the next node of the head will be the last node of the reversed Linked List and the head will be pointing to this node.
  • But for the complete reversal of the Linked List, the head should be the last node. So, we do the following:
    • head.next.next = head, where head.next is the last node of the reverse Linked List.
    • head.next = NULL
  • Return the head pointer of the reversed Linked List i.e. return rest.

04 Approach

  • Initially, we will take three-pointers, current that points to the head of Linked List, prev, and nextNode points to null.
  • Then we will iterate over the linked list until the current is not equal to NULL and do the following update in every step of the iteration:
    • nextNode = current.next
    • current.next = prev
    • prev = current
    • current = nextNode
  • Now return the prev pointer which is now the head of reverse Linked List.

05 Approach

This approach is similar to the previous approach. While we are iterating the linked list we were swapping pointers using the third pointer, but in this approach, we will swap the pointers using only two pointers(one itself the given head) by using the property of XOR:

 

The property of XOR is:

 

  • X⊕X = 0
  • X⊕0 = X
  • X⊕Y = Y⊕X
  • (X⊕Y)⊕Z = X⊕(Y⊕Z)

 

Steps

 

  • Initially, we will take two-pointers, given head that points to the head of Linked List, prev, points to null.
  • Then we will iterate over the linked list until the current is not equal to NULL and do the following update in every step of the iteration:
    • prev = prev ⊕ head->next
    • head->next = prev ⊕ head->next
    • prev = prev ⊕ head->next
    • prev = prev ⊕ head
    • head= prev ⊕ head
    • prev = prev ⊕ head
  • Now return the prev pointer which is now the head of reverse Linked List.