Print the last k nodes of the linked list in reverse order

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 
Output: 8 2 12 9

Source: Amazon Interview Experience SDE Off Campus.



Recursive Approach: Recursively traverse the linked list. When returning from each recursive call keep track of the node number, considering the last node as number 1, second last as number 2 and so on. This counting could be tracked with the help of a global or pointer variable. With the help of this count variable, print the nodes having a node number less than or equal to k.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to print the last k nodes
// of linked list in reverse order
#include <bits/stdc++.h>
using namespace std;
  
// Structure of a node
struct Node {
    int data;
    Node* next;
};
  
// Function to get a new node
Node* getNode(int data)
{
    // allocate space
    Node* newNode = new Node;
  
    // put in data
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
  
// Function to print the last k nodes
// of linked list in reverse order
void printLastKRev(Node* head,
                     int& count, int k)
{
    // if list is empty
    if (!head)
        return;
  
    // Recursive call with the next node
    // of the list
    printLastKRev(head->next, count, k);
  
    // Count variable to keep track of
    // the last k nodes
    count++;
  
    // Print data
    if (count <= k)
        cout << head->data << " ";
}
  
// Driver code
int main()
{
    // Create list: 1->2->3->4->5
    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, count = 0;
  
    // print the last k nodes
    printLastKRev(head, count, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to print the last k nodes 
// of linked list in reverse order 
class GfG 
{
  
// Structure of a node 
static class Node 
    int data; 
    Node next; 
}
  
// Function to get a new node 
static Node getNode(int data) 
    // allocate space 
    Node newNode = new Node(); 
  
    // put in data 
    newNode.data = data; 
    newNode.next = null
    return newNode; 
  
static class C
{
    int count = 0;
}
  
// Function to print the last k nodes 
// of linked list in reverse order 
static void printLastKRev(Node head, C c, int k) 
    // if list is empty 
    if (head == null
        return
  
    // Recursive call with the next node 
    // of the list 
    printLastKRev(head.next, c, k); 
  
    // Count variable to keep track of 
    // the last k nodes 
    c.count++; 
  
    // Print data 
    if (c.count <= k) 
        System.out.print(head.data + " "); 
  
// Driver code 
public static void main(String[] args) 
    // Create list: 1->2->3->4->5 
    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;
    C c = new C();
  
    // print the last k nodes 
    printLastKRev(head, c, k); 
}
   
// This code is contributed by prerna saini

chevron_right


C#

// C# implementation to print the last k
// nodes of linked list in reverse order
using System;

class GFG
{

// Structure of a node
public class Node
{
public int data;
public Node next;
}

// Function to get a new node
static Node getNode(int data)
{
// allocate space
Node newNode = new Node();

// put in data
newNode.data = data;
newNode.next = null;
return newNode;
}

public class C
{
public int count = 0;
}

// Function to print the last k nodes
// of linked list in reverse order
static void printLastKRev(Node head, C c, int k)
{
// if list is empty
if (head == null)
return;

// Recursive call with the next
// node of the list
printLastKRev(head.next, c, k);

// Count variable to keep track
// of the last k nodes
c.count++;

// Print data
if (c.count <= k) Console.Write(head.data + " "); } // Driver code public static void Main(String []args) { // Create list: 1->2->3->4->5
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;
C c = new C();

// print the last k nodes
printLastKRev(head, c, k);
}
}

// This code is contributed by Arnab Kundu

Output:

5 4 3 2

Time Complexity: O(n).

Iterative Approach: The idea is to use Stack Data Structure.

  1. Push all linked list nodes to a stack.
  2. Pop k nodes from stack and print them.

Time Complexity: O(n).

Two Pointer Approach The idea is similar to find k-th node from end of linked list.

  1. Move first pointer k nodes ahead.
  2. Now start another pointer, second from head.
  3. When first pointer reaches end, second pointer points to k-th node.
  4. Finally using the second pointer, print last k nodes.

Time Complexity: O(n).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : prerna saini, andrew1234