×

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the interviewee's logical reasoning, critical thinking, and problem-solving abilities. The n-th node first from the end of a LinkedList will therefore be obtained in this section using various methods and logic. Additionally, we'll write Java programs for the same.

Problem  Statement

There is a value N and a singly linked list provided. The number of the Nth node out from the end must be calculated as our task. The end indicates that the reference point is taken as from the rightmost node, which complicates the issue. It's because there is only one way to explore a singly linked list. Keep in mind that N will always be less than as well as equal to the length of a linked list. Take note of the examples below.

Example 1:

Input: 18 -> 57 -> 47 -> 33 -> 11 -> 75 -> 65 -> 98 -> NULL, N = 2

Output: 65

Explanation: Node 65 is the second node as from the end of the provided linked list.

Example 2:

Input: 19 -> 56 -> 154 -> 12 -> 10 -> 70 -> 66 -> 30 -> NULL, N = 5

Output: 12

Explanation: Node 12 is the fifth node as from the end of the provided linked list.

Solution to the Problem Using Stack as an approach

The Last in First Out (LIFO) principle governs how stacks operate. So, whenever it's necessary to calculate the nth node as from the end of such a linked list, a stack comes in handy. All we need to do is place every node in the stack, then take out n nodes at a time. We provide the value of an nth deleted node.

NthEndNode.java

import java.util.Stack;  
class LLNode    
{    
int v;    
LLNode next;    
// Taking n as its argument 
// in the class node function Object() { [native code] }  
LLNode(int n)    
{    
this.v = n;    
this.next = null;    
}    
}   
public class NthEndNode   
{  
public int findNthFromEnd(LLNode h1, int n1)  
{  
Stack<Integer> st = new Stack<Integer>();  
LLNode ptr = h1;  
while(ptr != null)  
{  
    st.push(ptr.v);  
    ptr = ptr.next;  
}  
int v = 0;  
while(n1 != 0)  
{  
v = st.peek();  
st.pop();  
n1 = n1 - 1;  
}  
return v;  
}  
// Driver code  
public static void main(String[] args)  
{  
// creating an instance for the class NthEndNode  
NthEndNode o = new NthEndNode();  
// Giving the first input  
int n1 = 2;  
// the topmost node in the linked list, or its first node.   
LLNode h1 = new LLNode(19);  
// remainder of the linked list's nodes   
h1.next = new LLNode(57);    
h1.next.next = new LLNode(44);    
h1.next.next.next = new LLNode(34);    
h1.next.next.next.next = new LLNode(11);    
h1.next.next.next.next.next = new LLNode(77);
// h1.next.next.next.next.next.next.next = new LLNode(30);      
int a = o.findNthFromEnd(h1, n1);  
System.out.println(" Regarding the subsequent linked list ");  
while(h1 != null)  
{  
    System.out.print(h1.v + " ");  
    h1 = h1.next;  
}  
System.out.println();  
System.out.println(" The " + n1 + "nd node from the end of the LinkedList is: " + a);  
h1 = null;  
System.out.println( "\n");  
// input 2  
n1 = 5;  
// the topmost node in the linked list, or its first node.   
h1 = new LLNode(19);  
// remainder of the linked list's nodes    
h1.next = new LLNode(11);    
h1.next.next = new LLNode(56);    
h1.next.next.next = new LLNode(51);    
h1.next.next.next.next = new LLNode(32);    
h1.next.next.next.next.next = new LLNode(10);  
h1.next.next.next.next.next.next = new LLNode(70);   
// h1.next.next.next.next.next.next.next = new LLNode(30);  
a = o.findNthFromEnd(h1, n1);  
System.out.println(" Regarding the subsequent linked list  ");  
while(h1 != null)  
{  
    System.out.print(h1.v + " ");  
    h1 = h1.next;  
}  
System.out.println();  
System.out.println(" The " + n1 + "th node from the end of the LinkedList is: " + a);  
}  
}  

Output:

Nth node from the end of the Linked list in Java

Analysis of Complexity: The aforementioned program's time complexity is O (s). The program's space complexity is O(s), where s is indeed the total number of nodes in the specified linked list, and because the program uses a stack to store the nodes.

In terms of spatial complexity, we can still make some improvements. Keep in mind the following strategy.

Utilizing the Size of a Linked List as a Method

We are aware that moving from left to right via a singly linked list is simple. In the above method, we used the stack to traverse the linked list in the reverse direction. If we closely examine the linked list, we can see that the second node from the final is the fifth node from the start, and the fifth node as from the end is the second node from the start (see example 1). (see example 2). We may determine a formula for calculating the nth node from the finish by considering these two examples. The equation is:

The node that is (s - n + 1)th from the beginning, which is the number of nodes in the linked list, is the node that is (nth) from the end. As a result, the 6 - 2 + 1 = 5th node from the beginning is the 2nd node from the finish (6 is the size of the linked list mentioned in example 1). The strategy is therefore straightforward. The linked list's total size needs to be calculated, and that's all. Watch the program that follows.

// NthEndNode1.java

import java.util.Stack;  
class LLNode    
{    
int val;    
LLNode next;    
// Taking n as its argument 
// in the class node function Object() 
// { [native code] } 
LLNode(int n)    
{    
this.val = n;    
this.next = null;    
}    
}   
public class NthEndNode1   
{  
public int findNthFromEnd(LLNode h1, int n1)  
{  
LLNode p = h1;  
int s = 0;  
// calculating the size of the linked list  
while(p != null)  
{  
    s = s + 1;  
    p = p.next;  
}  
// According to the strategy, we must begin at the beginning.
int v = s - n1 + 1;  
// Because h1 has already been pointing to the root node, 
// v!= 1.  
while(v != 1)  
{  
    v = v - 1;  
    h1 = h1.next;  
}  
return h1.val;  
}  
// Driver code  
public static void main(String[] args)  
{  
// creating an instance for the class NthEndNode1  
NthEndNode1 o = new NthEndNode1();  
// Giving the first input  
int n1 = 2;  
// the topmost node in the linked list, or its first node.   
LLNode h1 = new LLNode(19);  
// remainder of the linked list's nodes   
h1.next = new LLNode(57);    
h1.next.next = new LLNode(44);    
h1.next.next.next = new LLNode(34);    
h1.next.next.next.next = new LLNode(11);    
h1.next.next.next.next.next = new LLNode(77);
// h1.next.next.next.next.next.next.next = new LLNode(30);      
int a = o.findNthFromEnd(h1, n1);  
System.out.println(" Regarding the subsequent linked list ");  
while(h1 != null)  
{  
    System.out.print(h1.val + " ");  
    h1 = h1.next;  
}  
System.out.println();  
System.out.println(" The " + n1 + "nd node from the end of the LinkedList is: " + a);  
h1 = null;  
System.out.println( "\n");  
// input 2  
n1 = 5;  
// the topmost node in the linked list, or its first node.   
h1 = new LLNode(19);  
// remainder of the linked list's nodes    
h1.next = new LLNode(11);    
h1.next.next = new LLNode(56);    
h1.next.next.next = new LLNode(51);    
h1.next.next.next.next = new LLNode(32);    
h1.next.next.next.next.next = new LLNode(10);  
h1.next.next.next.next.next.next = new LLNode(70);   
// h1.next.next.next.next.next.next.next = new LLNode(30);  
a = o.findNthFromEnd(h1, n1);  
System.out.println(" Regarding the subsequent linked list  ");  
while(h1 != null)  
{  
    System.out.print(h1.val + " ");  
    h1 = h1.next;  
}  
System.out.println();  
System.out.println(" The " + n1 + "th node from the end of the LinkedList is: " + a);  
}  
}  

Output:

Nth node from the end of the Linked list in Java

Analysis of Complexity: The program mentioned above has the same level of temporal complexity as the one before it. The program's space complexity is O because it doesn't use any data structures (1).


Related Topics

Java String replaceFirst() method

This method replace the first substring with user specified String. Syntax: public String replaceFirst(String Regexp, String Replacement) Parameter: Regexp : Regular Expression Replacement : the String which would replace found expression Return: a string Java String replaceFirst()...

1 minute read.

Pyramid Program in Java

Pyramid Program in Java In the previous section, we have discussed about the number pattern programs in Java. The logic for the number pattern and pyramid pattern is the same except...

2 minutes read.

Java Control Statements

Control Statements: Control statements in Java can also be referred to as decision-making while dealing with different problems. Control statements are helpful to sort out the flow of the program or...

7 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Java Math.multiplyExact() method in Java

Java has an inbuilt math function called Math.multiplyExact() that returns the sum of the parameters. If the result exceeds an integer, an exception is thrown. There is no need to...

2 minutes read.

ArrayList VS Linked List

ArrayList VS Linked List Both the ArrayList and LinkedList implements the List interface, and they have some differences as well as some similarities between them. The internal working and performance of both vary significantly. Let’s...

7 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

How to Convert int to double in Java

How to Convert int to double in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

What is string in Java why it's immutable

String in Java Strings are a series of characters commonly used in Java language, which are considered objects in the Java. To create and manipulate strings, java will provide the String...

4 minutes read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.

Skyline Problem in Java

The skyline of a city is the outer edge of the pattern created by all of its structures when viewed from a distance. Return the skyline that these buildings together...

4 minutes read.