How do you identify the last node in the singly linked list?

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 : 4
Input : List = 2 -> 4 -> 6 -> 8 -> 33 -> 67 -> NULL
Output : 33

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to traverse the linked list following the below approach:

  1. If the list is empty or contains less than 2 elements, return false.
  2. 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.
  3. If the current node is the second last node, print the node otherwise move to the next node.
  4. 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
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 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
Python3




# Python3 program to find the second last node
# of a linked list in single traversal
import math
# Link list node
class Node:
def __init__[self, data]:
self.data = data
self.next = None
# Function to find the second last
# node of the linked list
def findSecondLastNode[head]:
temp = head
# If the list is empty or
# contains less than 2 nodes
if [temp == None or temp.next == None]:
return -1
# Traverse the linked list
while [temp != None]:
# Check if the current node is the
# second last node or not
if [temp.next.next == None]:
return temp.data
# If not then move to the next node
temp = temp.next
# Function to push node at head
def push[head, new_data]:
new_node = Node[new_data]
#new_node.data = new_data
new_node.next = head
head = new_node
return head
# Driver code
if __name__ == '__main__':
# Start with the empty list
head = None
# Use push[] function to construct
# the below list 8 . 23 . 11 . 29 . 12
head = push[head, 12]
head = push[head, 29]
head = push[head, 11]
head = push[head, 23]
head = push[head, 8]
print[findSecondLastNode[head]]
# This code is contributed by Srathore
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
Javascript




// Javascript program to find the second last node
// of a linked list in single traversal
// Link list node
class Node {
constructor[data] {
this.data = data;
this.next = null;
}
}
// method to find the second last
// node of the linked list
function findSecondLastNode[ ptr]
{
var 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
function push[ data]
{
var new_node = new Node[data] ;
new_node.next = head ;
head = new_node ;
return head ;
}
// Driver Code
var head = null ;
/* Use push[] function to construct
the below list 8 -> 23 -> 11 -> 29 -> 12 */
head = push[12];
head = push[29];
head = push[11];
head = push[23];
head = push[8];
document.write[findSecondLastNode[head]];
// This code is contributed by jana_sayantan.
Output: 29

Time complexity : O[n]




Article Tags :
Linked List
Technical Scripter
Order-Statistics
Technical Scripter 2018
Practice Tags :
Linked List
Read Full Article

Program for n’th node from the end of a Linked List

Given a Linked List and a number n, write a function that returns the value at the n’th node from the end of the Linked List.
For example, if the input is below list and n = 3, then output is “B”

What is a singly linked list?

A singly linked list is a type of linked list that is unidirectional, that is, it can be traversed in only one direction from head to the last node [tail].

Each element in a linked list is called a node. A single node contains data and a pointer to the next node which helps in maintaining the structure of the list.

The first node is called the head; it points to the first node of the list and helps us access every other element in the list. The last node, also sometimes called the tail, points to NULL which helps us in determining when the list ends.

Here given code implementation process.

    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

Video liên quan

Bài mới nhất

Chủ Đề