Given two Linked list L1 and L2. The second list L2 contains all the nodes of L1 along with 1 extra node. The task is to find that extra node.
Input: L1 = 17 -> 7 -> 6 -> 16
L2 = 17 -> 7 -> 6 -> 16 -> 15
Element 15 is not present in the L1 list
Input: L1 = 10 -> 15 -> 5
L2 = 10 -> 100 -> 15 -> 5
- Run nested loops and find the nodes in L2 which is not present in L1.
- The time complexity of this approach will be O(N2) where N is the length of the linked list.
- If all the nodes of the L1 and L2 are XORed together then every node of A will give 0 with its occurrence in L2 and the extra element say X when XORed with 0 will give (X XOR 0) = X which is the result.
Below is the implementation of the above approach:
Time Complexity: O(N)
Space Complexity: O(1)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find the fractional (or n/k - th) node in linked list
- Find first node of loop in a linked list
- Find the balanced node in a Linked List
- Find modular node in a linked list
- Length of longest palindrome list in a linked list using O(1) extra space
- Find the second last node of a linked list in single traversal
- Find kth node from Middle towards Head of a Linked List
- Recursive Approach to find nth node from the end in the linked list
- Find the largest node in Doubly linked list
- Create new linked list from two given linked list with greater element at each node
- Print reverse of a Linked List without extra space and modifications
- Swap Kth node from beginning with Kth node from end in a Doubly Linked List
- Swap Kth node from beginning with Kth node from end in a Linked List
- Find pair for given sum in a sorted singly linked without extra space
- Remove every k-th node of the linked list
- Program for n'th node from the end of a Linked List
- Linked List | Set 2 (Inserting a node)
- Remove first node of the linked list
- Squareroot(n)-th node in a Linked List
- Linked List | Set 3 (Deleting a node)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Rajput-Ji