原文:

给定一个包含n个节点和正整数k的链表应小于或等于n。任务是打印该节点的最后k个节点。 以相反的顺序列出。

示例

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

来源:。

递归方法:递归遍历链表。 从每个递归调用返回时,请跟踪节点编号,将最后一个节点视为编号 1,将倒数第二个节点视为编号 2,依此类推。 可以借助全局变量或指针变量来跟踪此计数。 借助此count变量,打印节点号小于或等于k的节点。

下面是上述方法的实现:

c

// c   implementation to print the last k nodes 
// of linked list in reverse order 
#include  
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; 
} 

java

// 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 

python

# python implementation to print the last k nodes  
# of linked list in reverse order  
# node class  
class node:  
    # function to initialise the node object  
    def __init__(self, data):  
        self.data = data # assign data  
        self.next =none
# function to get a new node  
def getnode(data):  
    # allocate space  
    newnode = node(0)  
    # put in data  
    newnode.data = data  
    newnode.next = none
    return newnode  
class c:  
    def __init__(self, data):  
        self.count = data  
# function to print the last k nodes  
# of linked list in reverse order  
def printlastkrev(head, c, k):  
    # if list is empty  
    if (head == none):  
        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 = c.count   1
    # print data  
    if (c.count <= k) : 
        print(head.data, end = " ")  
# driver code  
# create list: 1->2->3->4->5  
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
c = c(0) 
# print the last k nodes  
printlastkrev(head, c, k)  
# this code is contributed by arnab kundu 

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 

输出

5 4 3 2

时间复杂度o(n)

迭代方法:想法是使用。

  1. 将所有链表节点推入栈。

  2. 从栈中弹出k个节点并进行打印。

时间复杂度o(n)

双指针方法的想法类似于从的末尾找到第k个节点。

  1. 将第一个指针向前移动k个节点。

  2. 现在从头开始第二个指针。

  3. 当第一个指针到达末尾时,第二个指针指向第k个节点。

  4. 最后使用第二个指针,打印最后k个节点。

时间复杂度o(n)



如果您喜欢 geeksforgeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 geeksforgeeks pg电子试玩链接主页上,并帮助其他 geeks。

如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。