Print the last k nodes of the linked list in reverse order | Iterative Approaches
Last Updated :
22 Nov, 2021
Given a linked list containing N nodes and a positive integer K where K should be less than or equal to N. The task is to print the last K nodes of the list in reverse order.
Examples:
Input : list: 1->2->3->4->5, K = 2
Output : 5 4
Input : list: 3->10->6->9->12->2->8, K = 4
Output : 8 2 12 9
The solution discussed in previous post uses recursive approach. The following article discusses three iterative approaches to solve the above problem.
Approach 1: The idea is to use stack data structure. Push all the linked list nodes data value to stack and pop first K elements and print them.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode( int data)
{
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void printLastKRev(Node* head, int k)
{
if (!head)
return ;
stack< int > st;
while (head) {
st.push(head->data);
head = head->next;
}
int cnt = 0;
while (cnt < k) {
cout << st.top() << " " ;
st.pop();
cnt++;
}
}
int main()
{
Node* head = getNode(1);
head->next = getNode(2);
head->next->next = getNode(3);
head->next->next->next = getNode(4);
head->next->next->next->next = getNode(5);
int k = 4;
printLastKRev(head, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static void printLastKRev(Node head, int k)
{
if (head == null )
return ;
Stack<Integer> st = new Stack<Integer>();
while (head != null )
{
st.push(head.data);
head = head.next;
}
int cnt = 0 ;
while (cnt < k)
{
System.out.print(st.peek() + " " );
st.pop();
cnt++;
}
}
public static void main(String[] args)
{
Node head = getNode( 1 );
head.next = getNode( 2 );
head.next.next = getNode( 3 );
head.next.next.next = getNode( 4 );
head.next.next.next.next = getNode( 5 );
int k = 4 ;
printLastKRev(head, k);
}
}
|
Python3
import sys
import math
class Node:
def __init__( self ,data):
self .data = data
self . next = None
def getNode(data):
return Node(data)
def printLastKRev(head,k):
if not head:
return
stack = []
while (head):
stack.append(head.data)
head = head. next
cnt = 0
while (cnt < k):
print ( "{} " . format (stack[ - 1 ]),end = "")
stack.pop()
cnt + = 1
if __name__ = = '__main__' :
head = getNode( 1 )
head. next = getNode( 2 )
head. next . next = getNode( 3 )
head. next . next . next = getNode( 4 )
head. next . next . next . next = getNode( 5 )
k = 4
printLastKRev(head,k)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static void printLastKRev(Node head, int k)
{
if (head == null )
return ;
Stack< int > st = new Stack< int >();
while (head != null )
{
st.Push(head.data);
head = head.next;
}
int cnt = 0;
while (cnt < k)
{
Console.Write(st.Peek() + " " );
st.Pop();
cnt++;
}
}
public static void Main(String[] args)
{
Node head = getNode(1);
head.next = getNode(2);
head.next.next = getNode(3);
head.next.next.next = getNode(4);
head.next.next.next.next = getNode(5);
int k = 4;
printLastKRev(head, k);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function getNode(data) {
var newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
function printLastKRev(head , k) {
if (head == null )
return ;
var st = [];
while (head != null ) {
st.push(head.data);
head = head.next;
}
var cnt = 0;
while (cnt < k) {
document.write(st.pop() + " " );
cnt++;
}
}
var head = getNode(1);
head.next = getNode(2);
head.next.next = getNode(3);
head.next.next.next = getNode(4);
head.next.next.next.next = getNode(5);
var k = 4;
printLastKRev(head, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
The auxiliary space of the above approach can be reduced to O(k). The idea is to use two pointers. Place first pointer to beginning of the list and move second pointer to k-th node form beginning. Then find k-th node from end using approach discussed in this article: Find kth node from end of linked list. After finding kth node from end push all the remaining nodes in the stack. Pop all elements one by one from stack and print them.
Below is the implementation of the above efficient approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode( int data)
{
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void printLastKRev(Node* head, int k)
{
if (!head)
return ;
stack< int > st;
Node *first = head, *sec = head;
int cnt = 0;
while (cnt < k) {
sec = sec->next;
cnt++;
}
while (sec) {
first = first->next;
sec = sec->next;
}
while (first) {
st.push(first->data);
first = first->next;
}
while (!st.empty()) {
cout << st.top() << " " ;
st.pop();
}
}
int main()
{
Node* head = getNode(1);
head->next = getNode(2);
head->next->next = getNode(3);
head->next->next->next = getNode(4);
head->next->next->next->next = getNode(5);
int k = 4;
printLastKRev(head, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static void printLastKRev(Node head, int k)
{
if (head == null )
return ;
Stack<Integer> st = new Stack<Integer>();
Node first = head, sec = head;
int cnt = 0 ;
while (cnt < k)
{
sec = sec.next;
cnt++;
}
while (sec != null )
{
first = first.next;
sec = sec.next;
}
while (first != null )
{
st.push(first.data);
first = first.next;
}
while (!st.empty())
{
System.out.print(st.peek() + " " );
st.pop();
}
}
public static void main(String[] args)
{
Node head = getNode( 1 );
head.next = getNode( 2 );
head.next.next = getNode( 3 );
head.next.next.next = getNode( 4 );
head.next.next.next.next = getNode( 5 );
int k = 4 ;
printLastKRev(head, k);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def getNode(data):
newNode = Node( 0 )
newNode.data = data
newNode. next = None
return newNode
def printLastKRev( head, k):
if (head = = None ):
return
st = []
first = head
sec = head
cnt = 0
while (cnt < k) :
sec = sec. next
cnt = cnt + 1
while (sec ! = None ):
first = first. next
sec = sec. next
while (first ! = None ):
st.append(first.data)
first = first. next
while ( len (st)):
print ( st[ - 1 ], end = " " )
st.pop()
head = getNode( 1 )
head. next = getNode( 2 )
head. next . next = getNode( 3 )
head. next . next . next = getNode( 4 )
head. next . next . next . next = getNode( 5 )
k = 4
printLastKRev(head, k)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
class Node
{
public int data;
public Node next;
};
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static void printLastKRev(Node head, int k)
{
if (head == null )
return ;
Stack< int > st = new Stack< int >();
Node first = head, sec = head;
int cnt = 0;
while (cnt < k)
{
sec = sec.next;
cnt++;
}
while (sec != null )
{
first = first.next;
sec = sec.next;
}
while (first != null )
{
st.Push(first.data);
first = first.next;
}
while (st.Count != 0)
{
Console.Write(st.Peek() + " " );
st.Pop();
}
}
public static void Main(String[] args)
{
Node head = getNode(1);
head.next = getNode(2);
head.next.next = getNode(3);
head.next.next.next = getNode(4);
head.next.next.next.next = getNode(5);
int k = 4;
printLastKRev(head, k);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function getNode(data) {
var newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
function printLastKRev(head, k) {
if (head == null ) return ;
var st = [];
var first = head,
sec = head;
var cnt = 0;
while (cnt < k) {
sec = sec.next;
cnt++;
}
while (sec != null ) {
first = first.next;
sec = sec.next;
}
while (first != null ) {
st.push(first.data);
first = first.next;
}
while (st.length != 0) {
document.write(st[st.length - 1] + " " );
st.pop();
}
}
var head = getNode(1);
head.next = getNode(2);
head.next.next = getNode(3);
head.next.next.next = getNode(4);
head.next.next.next.next = getNode(5);
var k = 4;
printLastKRev(head, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(k)
Approach-2:
- Count the number of nodes in the linked list.
- Declare an array with the number of nodes as its size.
- Start storing the value of nodes of the linked list from the end of the array i.e. reverse manner.
- Print k values from starting of the array.
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode( int data){
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void printLastKRev(Node* head,
int & count, int k) {
struct Node* cur = head;
while (cur != NULL){
count++;
cur = cur->next;
}
int arr[count], temp = count;
cur = head;
while (cur != NULL){
arr[--temp] = cur->data;
cur = cur->next;
}
for ( int i = 0; i < k; i++)
cout << arr[i] << " " ;
}
int main()
{
Node* head = getNode(1);
head->next = getNode(2);
head->next->next = getNode(3);
head->next->next->next = getNode(4);
head->next->next->next->next = getNode(5);
head->next->next->next->next->next = getNode(10);
int k = 4, count = 0;
printLastKRev(head, count, k);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static void printLastKRev(Node head,
int count, int k)
{
Node cur = head;
while (cur != null )
{
count++;
cur = cur.next;
}
int []arr = new int [count];
int temp = count;
cur = head;
while (cur != null )
{
arr[--temp] = cur.data;
cur = cur.next;
}
for ( int i = 0 ; i < k; i++)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
Node head = getNode( 1 );
head.next = getNode( 2 );
head.next.next = getNode( 3 );
head.next.next.next = getNode( 4 );
head.next.next.next.next = getNode( 5 );
head.next.next.next.next.next = getNode( 10 );
int k = 4 , count = 0 ;
printLastKRev(head, count, k);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def getNode(data):
newNode = Node(data)
return newNode
def printLastKRev(head, count,k):
cur = head;
while (cur ! = None ):
count + = 1
cur = cur. next ;
arr = [ 0 for i in range (count)]
temp = count;
cur = head;
while (cur ! = None ):
temp - = 1
arr[temp] = cur.data;
cur = cur. next ;
for i in range (k):
print (arr[i], end = ' ' )
if __name__ = = '__main__' :
head = getNode( 1 );
head. next = getNode( 2 );
head. next . next = getNode( 3 );
head. next . next . next = getNode( 4 );
head. next . next . next . next = getNode( 5 );
head. next . next . next . next . next = getNode( 10 );
k = 4
count = 0 ;
printLastKRev(head, count, k);
|
C#
using System;
class GFG
{
class Node
{
public int data;
public Node next;
};
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static void printLastKRev(Node head,
int count, int k)
{
Node cur = head;
while (cur != null )
{
count++;
cur = cur.next;
}
int []arr = new int [count];
int temp = count;
cur = head;
while (cur != null )
{
arr[--temp] = cur.data;
cur = cur.next;
}
for ( int i = 0; i < k; i++)
Console.Write(arr[i] + " " );
}
public static void Main(String[] args)
{
Node head = getNode(1);
head.next = getNode(2);
head.next.next = getNode(3);
head.next.next.next = getNode(4);
head.next.next.next.next = getNode(5);
head.next.next.next.next.next = getNode(10);
int k = 4, count = 0;
printLastKRev(head, count, k);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function getNode( data)
{
var newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
function printLastKRev( head, count, k)
{
var cur = head;
while (cur != null )
{
count++;
cur = cur.next;
}
let arr = new Array(count);
let temp = count;
cur = head;
while (cur != null )
{
arr[--temp] = cur.data;
cur = cur.next;
}
for (let i = 0; i < k; i++)
document.write(arr[i] + " " );
}
var head = getNode(1);
head.next = getNode(2);
head.next.next = getNode(3);
head.next.next.next = getNode(4);
head.next.next.next.next = getNode(5);
head.next.next.next.next.next = getNode(10);
let k = 4, count = 0;
printLastKRev(head, count, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach-3: The idea is to first reverse the linked list iteratively as discussed in following post: Reverse a linked list. After reversing print first k nodes of the reversed list. After printing restore the list by reversing the list again.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode( int data)
{
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
Node* reverseLL(Node* head)
{
if (!head || !head->next)
return head;
Node *prev = NULL, *next = NULL, *curr = head;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
void printLastKRev(Node* head, int k)
{
if (!head)
return ;
head = reverseLL(head);
Node* curr = head;
int cnt = 0;
while (cnt < k) {
cout << curr->data << " " ;
cnt++;
curr = curr->next;
}
head = reverseLL(head);
}
int main()
{
Node* head = getNode(1);
head->next = getNode(2);
head->next->next = getNode(3);
head->next->next->next = getNode(4);
head->next->next->next->next = getNode(5);
int k = 4;
printLastKRev(head, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static Node reverseLL(Node head)
{
if (head == null || head.next == null )
return head;
Node prev = null , next = null , curr = head;
while (curr != null )
{
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
static void printLastKRev(Node head, int k)
{
if (head == null )
return ;
head = reverseLL(head);
Node curr = head;
int cnt = 0 ;
while (cnt < k)
{
System.out.print(curr.data + " " );
cnt++;
curr = curr.next;
}
head = reverseLL(head);
}
public static void main(String[] args)
{
Node head = getNode( 1 );
head.next = getNode( 2 );
head.next.next = getNode( 3 );
head.next.next.next = getNode( 4 );
head.next.next.next.next = getNode( 5 );
int k = 4 ;
printLastKRev(head, k);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def getNode(data):
newNode = Node(data)
return newNode
def reverseLL(head):
if ( not head or not head. next ):
return head
prev = None
next = None
curr = head;
while (curr):
next = curr. next
curr. next = prev
prev = curr
curr = next
return prev
def printLastKRev(head, k):
if ( not head):
return
head = reverseLL(head)
curr = head
cnt = 0
while (cnt < k):
print (curr.data, end = ' ' )
cnt + = 1
curr = curr. next
head = reverseLL(head)
if __name__ = = '__main__' :
head = getNode( 1 )
head. next = getNode( 2 )
head. next . next = getNode( 3 )
head. next . next . next = getNode( 4 )
head. next . next . next . next = getNode( 5 )
k = 4
printLastKRev(head, k)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static Node reverseLL(Node head)
{
if (head == null || head.next == null )
return head;
Node prev = null , next = null , curr = head;
while (curr != null )
{
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
static void printLastKRev(Node head, int k)
{
if (head == null )
return ;
head = reverseLL(head);
Node curr = head;
int cnt = 0;
while (cnt < k)
{
Console.Write(curr.data + " " );
cnt++;
curr = curr.next;
}
head = reverseLL(head);
}
public static void Main(String[] args)
{
Node head = getNode(1);
head.next = getNode(2);
head.next.next = getNode(3);
head.next.next.next = getNode(4);
head.next.next.next.next = getNode(5);
int k = 4;
printLastKRev(head, k);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
}
function getNode(data)
{
let newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
function reverseLL(head)
{
if (head == null || head.next == null )
return head;
let prev = null , next = null , curr = head;
while (curr != null )
{
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
function printLastKRev(head,k)
{
if (head == null )
return ;
head = reverseLL(head);
let curr = head;
let cnt = 0;
while (cnt < k)
{
document.write(curr.data + " " );
cnt++;
curr = curr.next;
}
head = reverseLL(head);
}
let head = getNode(1);
head.next = getNode(2);
head.next.next = getNode(3);
head.next.next.next = getNode(4);
head.next.next.next.next = getNode(5);
let k = 4;
printLastKRev(head, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Print the last k nodes of the linked list in reverse order | Recursive approach
Given a linked list containing N nodes and a positive integer k should be less than or equal to N. The task is to print the last k nodes of the list in reverse order. Examples: Input: list: 1->2->3->4->5, k = 2 Output: 5 4 Input: list: 3->10->6->9->12->2->8, k = 4 Outpu
7 min read
Reverse the order of all nodes at even position in given Linked List
Given a linked list A[] of N integers, the task is to reverse the order of all integers at an even position. Examples: Input: A[] = 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULLOutput: 1 6 3 4 5 2Explanation: Nodes at even position in the given linked list are 2, 4 and 6. So, after reversing
10 min read
Find the product of last N nodes of the given Linked List
Given a linked list and a number N. Find the product of last n nodes of the linked list. Constraints : 0 <= N <= number of nodes in the linked list. Examples: Input : List = 10->6->8->4->12, N = 2 Output : 48 Explanation : Product of last two nodes: 12 * 4 = 48 Input : List = 15-
15+ min read
Reverse a Linked List in groups of given size (Iterative Approach)
Given a Singly linked list containing n nodes. The task is to reverse every group of k nodes in the list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should be considered as a group and must be reversed. Examples: Input: head: 1 -> 2 -> 3 -> 4 -> 5 -
10 min read
An interesting method to print reverse of a linked list
We are given a linked list, we need to print the linked list in reverse order.Examples: Input : list : 5-> 15-> 20-> 25 Output : Reversed Linked list : 25-> 20-> 15-> 5Input : list : 85-> 15-> 4-> 20 Output : Reversed Linked list : 20-> 4-> 15-> 85Input : list : 8
7 min read
Find the sum of last n nodes of the given Linked List
Given a linked list and a number n. Find the sum of the last n nodes of the linked list.Constraints: 0 <= n <= number of nodes in the linked list. Examples: Input : 10->6->8->4->12, n = 2 Output : 16 Sum of last two nodes: 12 + 4 = 16 Input : 15->7->9->5->16->14, n =
15+ min read
Append the last M nodes to the beginning of the given linked list.
Given a linked list and an integer M, the task is to append the last M nodes of the linked list to the front.Examples: Input: List = 4 -> 5 -> 6 -> 1 -> 2 -> 3 -> NULL, M = 3 Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULLInput: List = 8 -> 7 -> 0 -> 4 -> 1
13 min read
Recursive Approach to find nth node from the end in the linked list
Find the nth node from the end in the given linked list using a recursive approach. Examples: Input : list: 4->2->1->5->3 n = 2 Output : 5 Algorithm: findNthFromLast(head, n, count, nth_last) if head == NULL then return findNthFromLast(head->next, n, count, nth_last) count = count + 1
8 min read
Print the alternate nodes of linked list (Iterative Method)
Given a linked list, print the alternate nodes of the linked list. Examples: Input : 1 -> 8 -> 3 -> 10 -> 17 -> 22 -> 29 -> 42 Output : 1 -> 3 -> 17 -> 29 Alternate nodes : 1 -> 3 -> 17 -> 29 Input : 10 -> 17 -> 33 -> 38 -> 73 Output : 10 -> 33 -
9 min read
Reverse alternate K nodes in a Singly Linked List - Iterative Solution
Given a linked list and an integer K, the task is to reverse every alternate K nodes.Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL, K = 3 Output: 3 2 1 4 5 6 9 8 7Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL, K =
12 min read