# Find the second last node of a linked list

Find out second last node of given singly linked list. We can solve this problem using single traversal of linked list. Before write actual algorithm consider following test cases.

Test Case 1) : When linked list are empty then show valid message.

2) When linked list are contain less than two nodes then display the (Only one node in this linked list).

3) Find the second last node in single traversal.

Suppose we are inserted the following (6, 3, 2, 7, 1, 9 ) node in a sequence.

Here given code implementation process.

``````//C Program
//Find the second last node of a linked list
#include<stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node{
int data;
struct Node*next;
};
//function prototype
void insert(struct Node**,int);
void display(struct Node*);
void second_last(struct Node*);
//insert Node element
//Create dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL){
printf("Memory overflow\n");
}else{
node->data=value;
node->next=NULL;
}else{
//find last node
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=node;
}
}
}
//Find the second last node of a linked list
void second_last(struct Node*temp){
if(temp==NULL){
}else if(temp->next==NULL){
printf("Only one node in this linked list");
}else{
//find second last node
while(temp!=NULL && temp->next!=NULL && temp->next->next!=NULL){
temp=temp->next->next;
}
printf("\nSecond last element : %d\n",temp->data );
}
}
//Display element of Node
void display(struct Node*temp){

if(temp==NULL){
}
while(temp!=NULL){
printf("%d  ",temp->data);
temp=temp->next;
}
}
int main(){
//create node pointer

//display all node
}``````

#### Output

``````6  3  2  7  1  9
Second last element : 1``````
``````//C++ Program
//Find the second last node of a linked list
#include<iostream>
using namespace std;

//create structure
struct Node{
int data;
struct Node*next;
};
public:
void insert(int);
void display();
void second_last();
};
}
//insert node at end of linked list
//Create dynamic node
struct Node*node=new Node;
if(node==NULL){
cout<<"Memory overflow\n";
}else{
node->data=value;
node->next=NULL;
//base condition
}else{
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=node;
}
}
}
//display all node value in linked list
}
else{
while(temp!=NULL){
//print node value
cout<<temp->data<<" ";
temp=temp->next;
}
}

}
//Find the second last node of a linked list
if(temp==NULL){
}else if(temp->next==NULL){
cout<<"Only one node in this linked list"<<endl;
}else{
//find second last node
while(temp!=NULL && temp->next!=NULL && temp->next->next!=NULL){
temp=temp->next->next;
}
cout<<"\nSecond last element : "<<temp->data <<endl;
}
}
int main(){

//create object
obj.insert(6);
obj.insert(3);
obj.insert(2);
obj.insert(7);
obj.insert(1);
obj.insert(9);

//display all node
obj.display();

obj.second_last();
return 0;
}``````

#### Output

``````Linked List : 6 3 2 7 1 9
Second last element : 1``````
``````//Java Program
//Find the second last node of a linked list
class Node {
public int data;
public Node next;
public Node(int value) {
data = value;
next = null;
}
}

//Class constructors
}
//insert element
public void insert(int value) {
//Create  node
Node node = new Node(value);

else {
//find last node
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
}

}
public void display() {
while (temp != null) {
System.out.print("  " + temp.data);
temp = temp.next;
}
} else {
}
}
//Find the second last node of a linked list
public void secondLast() {
if (temp == null) {
} else if (temp.next == null) {
System.out.print("Only one node in this linked list");
} else {
//find second last node
while (temp != null && temp.next != null && temp.next.next != null) {
temp = temp.next.next;
}
System.out.print("\nSecond last element : " + temp.data);
}
}

public static void main(String[] args) {

obj.insert(6);
obj.insert(3);
obj.insert(2);
obj.insert(7);
obj.insert(1);
obj.insert(9);

//display all node
obj.display();

obj.secondLast();
}
}``````

#### Output

``````Linked List Element :  6  3  2  7  1  9
Second last element : 1``````
``````#Python Program
#Find the second last node of a linked list
class Node:
def __init__(self,data):
self.data=data
self.next=None

def __init__(self):
#Assign default value

#insert new node to linked list
def insert(self,data):
node=Node(data)
node.next=None
else:
while temp.next!=None:
temp=temp.next
temp.next=node

def display(self):
return

while(temp!=None):
print(temp.data,end=" ")
temp=temp.next
def secondLast(self):
if(temp==None):
elif (temp.next==None):
print("Only one node in this linked list")
else:
#find second last node
while(temp!=None and temp.next!=None and temp.next.next!=None):
temp=temp.next.next

print("\nSecond last element : ",temp.data)

def main():
obj.insert(6)
obj.insert(3)
obj.insert(2)
obj.insert(7)
obj.insert(1)
obj.insert(9)

#display all node
obj.display()

obj.secondLast()

if __name__=="__main__":
main()``````

#### Output

``````Linked List Elements :
6 3 2 7 1 9
Second last element :  1``````
``````//C# Program
//Find the second last node of a linked list
using System;
//node class
public class Node{
public  int data;
public  Node next;
}
class Program
{
public Program(){
}
public void insert(int data){
Node newNode=new Node();
newNode.data=data;
newNode.next=null;
else{
//get last node
while(temp.next!=null){
temp=temp.next;
}
temp.next=newNode;
}
}
public void display(){
Console.Write("Empty List");
}else{
while(temp!=null){
Console.Write("  {0}",temp.data);
temp=temp.next;
}

}
}
//Find the second last node of a linked list
public void  secondLast(){
if(temp==null){
}else if(temp.next==null){
Console.WriteLine("Only one node in this linked list");
}else{
//find second last node
while(temp!=null && temp.next!=null && temp.next.next!=null){
temp=temp.next.next;
}
Console.WriteLine("\nSecond last element : "+temp.data);
}
}
static void Main()
{
Program obj=new Program();
obj.insert(6);
obj.insert(3);
obj.insert(2);
obj.insert(7);
obj.insert(1);
obj.insert(9);

//display all node
obj.display();

obj.secondLast();
}
}``````

#### Output

``````6 3 2 7 1 9
Second last element : 1``````
``````<?php
//Php program
//Find the second last node of a linked list
class Node
{
public \$data;
public \$next;
function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}

function __construct()
{
}
/*
* Append the Given data value at end of linked list
* Fun : insert
* Parm: data value
*@return None
*/
function insert(\$data)
{
\$newNode=new Node(\$data);
{
}else{
//find last node of linked list
while(\$temp->next!=NULL){
\$temp=\$temp->next;
}
\$temp->next=\$newNode;
}
}
//Display all inserted node in linked list
function display()
{
{
}
else{
while(\$temp!=NULL){
//display node value
echo "  ".\$temp->data;
\$temp=\$temp->next; //visit to next node
}
}
}
//Find the second last node of a linked list
function second_last()
{
if(\$temp==NULL)
{
}else if(\$temp->next==NULL)
{
echo "Only one node in this linked list";
}else
{
//find second last node
while(\$temp!=NULL && \$temp->next!=NULL && \$temp->next->next!=NULL)
{
\$temp=\$temp->next->next;
}
echo "<br>Second last element : ".\$temp->data;
}
}

}
function main(){
//Make a object of LinkedList class

/*
*Insert following nodes in linked list
*/
\$obj->insert(6);
\$obj->insert(3);
\$obj->insert(2);
\$obj->insert(7);
\$obj->insert(1);
\$obj->insert(9);
\$obj->display();
\$obj->second_last();
}
main();
?>``````

#### Output

``````Linked List : 6 3 2 7 1 9
Second last element : 1``````

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.