Input: 1 -> 2 -> 3 -> 4, N = 3
Output: 2
Explanation: Node 2 is the third node from the end of the linked list.
Input: 35 -> 15 -> 4 -> 20, N = 4
Output: 35
Explanation: Node 35 is the fourth node from the end of the linked list.
The idea is to maintain two pointers, say main_ptr and ref_ptr point to the head of Linked List and move ref_ptr to the Nth node from the head to ensure that the distance between main_ptr and ref_ptr is (N – 1). Now, move both the pointers simultaneously until ref_ptr reaches the last node. Since the distance between main_ptr and ref_ptr is (N – 1), so when ref_ptr will reach the last node, main_ptr will reach Nth node from the end of Linked List. Return the value of node pointed by main_ptr.
Unlock your potential with our DSA Self-Paced course, designed to help you master Data Structures and Algorithms at your own pace. In 90 days, you’ll learn the core concepts of DSA, tackle real-world problems, and boost your problem-solving skills, all at a speed that fits your schedule. With comprehensive lessons and practical exercises, this course will set you up for success in technical interviews and beyond.
And here's the challenge – join the Three 90 Challenge! Complete 90% of the course in 90 days, and you’ll earn a 90% refund. It's a great way to stay motivated, track your progress, and reward yourself for your dedication. Start the challenge today, and push your limits to achieve mastery in DSA!