Find the second last node of a linked list in single traversal
Given a linked list. The task is to find the second last node of the linked list using a single traversal only.
Examples:
Input : List = 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Output : 4Input : 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:
C++
// C++ program to find the second last node // of a linked list in single traversal #include <iostream> using namespace std; // Link list node struct Node { int data; struct Node* next; }; // Function to find the second last // node of the linked list int findSecondLastNode(struct Node* head) { struct Node* temp = head; // If the list is empty or contains less // than 2 nodes if (temp == NULL || temp->next == NULL) return -1; // Traverse the linked list while (temp != NULL) { // Check if the current node is the // second last node or not if (temp->next->next == NULL) return temp->data; // If not then move to the next node temp = temp->next; } } // Function to push node at head void push(struct Node** head_ref, int new_data) { Node* new_node = new Node; new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } // Driver code int main() { /* Start with the empty list */ struct Node* head = NULL; /* Use push() function to construct the below list 8 -> 23 -> 11 -> 29 -> 12 */ push(&head;, 12); push(&head;, 29); push(&head;, 11); push(&head;, 23); push(&head;, 8); cout << findSecondLastNode(head); return 0; } |
chevron_right
filter_none
Java
// Java program to find the second last node // of a linked list in single traversal // Linked list node class Node { int data; Node next; Node(int d) { this.data = d; this.next = null; } } class LinkedList { Node start; LinkedList() { start = null; } // Function to push node at head public void push(int data) { if(this.start == null) { Node temp = new Node(data); this.start = temp; } else { Node temp = new Node(data); temp.next = this.start; this.start = temp; } } // method to find the second last // node of the linked list public int findSecondLastNode(Node ptr) { Node temp = ptr; // If the list is empty or contains less // than 2 nodes if(temp == null || temp.next == null) return -1; // This loop stops at second last node while(temp.next.next != null) { temp = temp.next; } return temp.data; } // Driver code public static void main(String[] args) { LinkedList ll = new LinkedList(); /* Use push() function to construct the below list 8 -> 23 -> 11 -> 29 -> 12 */ ll.push(12); ll.push(29); ll.push(11); ll.push(23); ll.push(8); System.out.println(ll.findSecondLastNode(ll.start)); } } // This code is Contributed by Adarsh_Verma |
chevron_right
filter_none
C#
// C# program to find the second last node // of a linked list in single traversal using System; // Linked list node public class Node { public int data; public Node next; public Node(int d) { this.data = d; this.next = null; } } public class LinkedList { Node start; LinkedList() { start = null; } // Function to push node at head public void push(int data) { if(this.start == null) { Node temp = new Node(data); this.start = temp; } else { Node temp = new Node(data); temp.next = this.start; this.start = temp; } } // method to find the second last // node of the linked list public int findSecondLastNode(Node ptr) { Node temp = ptr; // If the list is empty or contains less // than 2 nodes if(temp == null || temp.next == null) return -1; // This loop stops at second last node while(temp.next.next != null) { temp = temp.next; } return temp.data; } // Driver code public static void Main() { LinkedList ll = new LinkedList(); /* Use push() function to construct the below list 8 -> 23 -> 11 -> 29 -> 12 */ ll.push(12); ll.push(29); ll.push(11); ll.push(23); ll.push(8); Console.WriteLine(ll.findSecondLastNode(ll.start)); } } // This code is contributed by PrinciRaj1992 |
chevron_right
filter_none
Output:
29
Time complexity : O(n)
Recommended Posts:
- Python program to find middle of a linked list using one traversal
- Find modular node in a linked list
- Find the fractional (or n/k - th) node in linked list
- Find the balanced node in a Linked List
- Find first node of loop in a linked list
- Find the largest node in Doubly linked list
- Find kth node from Middle towards Head of a Linked List
- Recursive Approach to find nth node from the end in the linked list
- Create new linked list from two given linked list with greater element at each node
- Swap Kth node from beginning with Kth node from end in a Linked List
- Implementing Iterator pattern of a single Linked List
- Circular Linked List | Set 2 (Traversal)
- Recursive insertion and traversal linked list
- Delete Nth node from the end of the given linked list
- Remove every k-th node of the 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 contribute@geeksforgeeks.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.



