Given a linked list. The task is to find the second last node of the linked list using a single traversal only.
Input : List = 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Output : 4
Input : List = 2 -> 4 -> 6 -> 8 -> 33 -> 67 -> NULL
Output : 33
The idea is to traverse the linked list following the below approach:
- If the list is empty or contains less than 2 elements, return false.
- Otherwise check if the current node is the second last node of the linked list or not. That is, if (current_node->next-next == NULL ) then the current node is the second last node.
- If the current node is the second last node, print the node otherwise move to the next node.
- Repeat the above two steps until the second last node is reached.
Below is the implementation of the above approach:
Time complexity : O(n)
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.
- Python program to find middle of a linked list using one traversal
- Find the fractional (or n/k - th) node in linked list
- Find extra node in the second Linked list
- Find modular node in a linked list
- Find the balanced node in a Linked List
- Find first node of loop in a linked list
- Recursive Approach to find nth node from the end in the linked list
- Find the largest node in Doubly linked list
- Find kth node from Middle towards Head of a Linked List
- Create new linked list from two given linked list with greater element at each node
- Implementing Iterator pattern of a single Linked List
- Circular Linked List | Set 2 (Traversal)
- Recursive insertion and traversal linked list
- 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
- Remove last node of the linked list
- Remove first node of the linked list
- Squareroot(n)-th node in a Linked List
- Remove Nth node from end of the Linked List
- Program for n'th node from the end of a Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.