Input: 4 –> 6 –> 7 –> 3, N = 1
Explanation: 1st node from the end is 3.
Input: 5 –> 8 –> 9, N = 4
Output: Wrong Input
Explanation: The given Xor Linked List contains only 3 nodes.
Approach: Follow the steps below to solve the problem:
- Traverse the first N nodes of the Linked List using a pointer, say curr.
- Use another pointer, say curr1, and traverse the linked list incrementing curr and curr1 by a node after every iteration.
- Iterate until the pointer curr exceeds the end of the List, i.e.NULL. Once reached, print the value of the node curr1 as the required answer.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.