Reverse a linked listGiven pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing the links between nodes. Show Examples:
Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)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->NULLRecommended: Please try your approach on {IDE} first, before moving on to the solution. We have seen how to reverse a linked list in article Reverse a linked list. In iterative method we had used 3 pointers prev, cur and next. Below is an interesting approach that uses only two pointers. The idea is to use XOR to swap pointers. C++
Java
Python3
C#
Javascript
Output:
Given linked list
85 15 4 20
Reversed Linked list
20 4 15 85
Time Complexity: O(n) C++
Python3
Output:
Given linked list
85 15 4 20
Reversed Linked list
20 4 15 85
Thanks to Abhay Yadav for suggesting this approach.
Article Tags :
Linked List
Bitwise-XOR Reverse Practice Tags :
Linked List Reverse Reversing a Linked List: Easy as 1, 2, 3Sergey Piterman Follow Aug 9, 2018 · 9 min read A common interview question asked at larger companies, reversing a linked list is a problem I’ve seen trip up a lot of engineers throughout my time at Outco. Often being unable to solve this problem stems from an engineer not being familiar with what linked lists are ahead of time, or not understanding the difference between a linked list and an array. This post assumes that you know what a linked list is, and some of its basic properties. If you don’t, here’s a good primer on Geeks for Geeks: Problem StatementGiven a pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing the links between nodes. Example: Input: [1,2,3,4,5,NULL] Input: [3,4,5] Recursive ApproachThe recursive approach to reverse a linked list is simple, just we have to divide the linked lists in two parts and i.e first node and the rest of the linked list, and then call the recursion for the other part by maintaining the connection. Recursive Approach Implementation Of Recursive ApproachC++ ImplementationListNode* reverseList(ListNode* head) { if(!head || !(head->next)) return head; auto res = reverseList(head->next); head->next->next = head; head->next = NULL; return res; }Java Implementationstatic class Node { int data; Node next; Node(int d) { data = d; next = null; } } static Node reverse(Node head) { if (head == null || head.next == null) return head; Node rest = reverse(head.next); head.next.next = head; head.next = null; return rest; }Python Implementationdef reverse(self, head): # If head is empty or has reached the list end if head is None or head.next is None: return head # Reverse the rest list rest = self.reverse(head.next) # Put first element at the end head.next.next = head head.next = None # Fix the header pointer return restTime complexity: O(N), Where N is the size of the linked list. |