How do you identify the last node in the singly linked list?
Find the second last node of a linked list in single traversalGiven a linked list. The task is to find the second last node of the linked list using a single traversal only. Show
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:
Below is the implementation of the above approach: C++
Java
Python3
C#
Javascript
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 ListGiven 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. 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.
|