Site icon

Find nth node from end / last node in a single linked list in java (example)

Fig 1: Single linked list
Fig 2: Nth node from end – Single linked list

Example – find n = 3 node from end of single linked list in java.
Step 1:  

Fig 3: Slow and fast reference pointing at head

Step 2:

Fig 4: fast reference moved by n=3

Step 3:

Fig 5 : slow and fast reference increment by one.

Step 4: 

Fig 6: Slow reference gives nth node as fast reaches end of linked list

Time complexity of algorithm is O(n).

Find nth element from end of single linked list (non recursive algorithm)

1.) FindNthNodeFromLast Class:

package org.learn.Question;

import org.learn.List.Node;

public class FindNthNodeFromLast {
	public static Node findNthNodeFromLast(Node head, int n) {
		if (null == head) {
			System.out.println("Single linked list is empty");
			return null;
		}

		Node slow = head;
		Node fast = head;
		int index = 0;
		while (index++ < n) {
			if (null == fast) {
				System.out.printf("\nn=%d is larger than length of linked list", n);
				return null;
			}
			fast = fast.next;
		}
		while (fast != null) {
			slow = slow.next;
			fast = fast.next;
		}
		return slow;
	}

	public static void insert(Node head, int data) {
		while (head.next != null)
			head = head.next;
		head.next = new Node(data);
	}

	public static void print(Node head) {
		while (head != null) {
			System.out.printf("%d ", head.data);
			head = head.next;
		}
		System.out.println("");
	}
}

2.) App Class:

We are performing the following operation in main method.

package org.learn.Client;

import org.learn.List.Node;
import org.learn.Question.FindNthNodeFromLast;

public class App {
	public static void main(String[] args) {
		int[] data = { 10, 20, 30, 40, 50 };
		Node head = new Node(data[0]);
		for (int count = 1; count < data.length; count++)
			FindNthNodeFromLast.insert(head, data[count]);
		System.out.printf("1. Single linked list is : ");
		FindNthNodeFromLast.print(head);

		int n = 1;
		Node nodeFromLast = FindNthNodeFromLast.findNthNodeFromLast(head, n);
		if (null != nodeFromLast) {
			System.out.printf("2. Node from last for n = %d is %d", n, nodeFromLast.data);
		}

		n = 3;
		nodeFromLast = FindNthNodeFromLast.findNthNodeFromLast(head, n);
		if (null != nodeFromLast) {
			System.out.printf("\n3. Node from last for n = %d is %d", n, nodeFromLast.data);
		}

		n = 5;
		nodeFromLast = FindNthNodeFromLast.findNthNodeFromLast(head, n);
		if (null != nodeFromLast) {
			System.out.printf("\n4. Node from last for n = %d is %d", n, nodeFromLast.data);
		}
	}
}

3.) Node Class:

package org.learn.List;

public class Node {
	public int data;
	public Node next;

	public Node(int num) {
		this.data = num;
		this.next = null;
	}
}

Output – find nth node from end of single linked list in java

1. Single linked list is : 10 20 30 40 50 
2. Node from last for n = 1 is 50
3. Node from last for n = 3 is 30
4. Node from last for n = 5 is 10

Download Code – find nth node from end of single linked list

 

Exit mobile version