Find the second last node of a linked list
The problem addressed in this code involves finding the second last node of a singly linked list. A singly linked list is a linear data structure in which each element is called a node and consists of data and a reference to the next node in the sequence.
Problem Statement and Description
Given a singly linked list, the task is to write a program that finds and prints the value of the second last node in the list. If the list contains only one node, or if it is empty, appropriate messages are displayed.
Example
Consider the following singly linked list:
6 → 3 → 2 → 7 → 1 → 9 → null
The second last node in this linked list has the value 1
.
Idea to Solve the Problem

To solve this problem, we can follow these steps:
- Traverse the linked list while keeping track of two pointers: one for the current node and another for the node before it.
- When the current node reaches the end of the list, the node before it will be the second last node.
Standard Pseudocode
Here's a high-level pseudocode representation of the algorithm to find the second last node of a singly linked list:
FindSecondLastNode(head):
if head is NULL:
Display "Empty linked list"
else if head.next is NULL:
Display "Only one node in this linked list"
else:
Initialize prevNode to NULL
Initialize currentNode to head
While currentNode.next is not NULL:
prevNode = currentNode
currentNode = currentNode.next
Display "Second last element is:", prevNode.data
Algorithm Explanation
- If the linked list is empty, display "Empty linked list."
- If there's only one node in the linked list, display "Only one node in this linked list."
- If there are at least two nodes in the linked list:
- Initialize two pointers:
prevNode
andcurrentNode
, both starting from the head of the linked list. - Traverse the linked list until
currentNode.next
is NULL (reaching the end). - During traversal, keep updating
prevNode
to be the previous node ofcurrentNode
. - The node pointed to by
prevNode
will be the second last node.
- Initialize two pointers:
- Display the value of the second last node.
Code Solution
-
1) Find second last element in linked list in java
2) Find second last element in linked list in c++
3) Find second last element in linked list in c
4) Find second last element of linked list in c#
5) Find second last element of linked list in php
6) Find second last element of linked list in python
7) Find second last element of linked list in swift
8) Find second last element of linked list in kotlin
9) Find second last element of linked list in ruby
10) Find second last element of linked list in scala
11) Find second last element of linked list in golang
12) Find second last element of linked list in node js
13) Find second last element of linked list in vb.net
14) Find second last element of linked list in typescript
Time Complexity
The time complexity of finding the second last node in a singly linked list using the provided algorithm is O(n), where 'n' is the number of nodes in the linked list. This is because the algorithm needs to traverse the entire list in order to reach the second last node.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment