[Programming question] enter a one-way linked list and output the last K nodes in the list.

Source: Internet
Author: User

13th questions (linked list ):
Question: enter a one-way linked list and output the k-th node in the list. The last 0th nodes of the linked list are linked lists.
The linked list node is defined as follows:
Struct listnode
Int m_nkey;
Listnode * m_pnext;


My idea: first flip the linked list, and then from the flip of the linked list head to the end of the number of K-1, return, flip the linked list again.

The Code is as follows: note that this idea is very poor. The reason for the difference is: If you only use the original method, first traverse the count, and then traverse and find the last K, you need to traverse twice. However, in my mind, if I flip the linked list twice, I have to traverse it twice. Also go to the K-1 step to find the last K. Tedious.

/* 13th question (linked list): Question: enter a one-way linked list and output the k-th node in the list. The last 0th nodes of the linked list are the tail pointer of the linked list. The linked list node is defined as follows: struct listnode {int m_nkey; listnode * m_pnext;}; Start time = 20: 40end time */# include <stdio. h> # include <stdlib. h> typedef struct listnode {int m_nkey; listnode * m_pnext;} listnode; listnode * reverselist (listnode * l) {listnode * PP = L; listnode * P = L-> m_pnext; PP-> m_pnext = NULL; while (P! = NULL) {listnode * P3 = p-> m_pnext; P-> m_pnext = pp; pp = P; P = P3;} return pp ;} listnode * getkthnodefromback (listnode * l, int K) {If (k = 0) return NULL; listnode * RL = reverselist (l); listnode * P = RL; for (INT I = 0; P! = NULL & I <K-1; I ++) // note that the detection K cannot exceed the length of the linked list, otherwise null {P = p-> m_pnext;} reverselist (RL) is returned ); return P;} void displaylist (listnode * l) {listnode * P = L; while (P! = NULL) {printf ("% d", p-> m_nkey); P = p-> m_pnext;} printf ("\ n ");} void createlist (listnode * & L) {int D; scanf ("% d", & D); If (D! = 0) {L = (listnode *) malloc (sizeof (listnode); L-> m_nkey = D; L-> m_pnext = NULL; createlist (L-> m_pnext) ;}} int main () {listnode * l = NULL; createlist (l); listnode * PK = getkthnodefromback (L, 3 ); displaylist (l); Return 0 ;}


Online thinking: http://blog.sina.com.cn/s/blog_60c8379d01013z0s.html

Analysis: Use two pointers: low and fast. First, point the fast pointer to the K element, and then low and fast traverse backward at the same time. When fast traverses to the end, low traverses the K at the bottom. Check whether the boundary and pointer are empty. Check whether K exceeds the length of the linked list.

LinkList.h #ifndef LINKLIST_H#define LINKLIST_H#include <stdlib.h>#include <stdio.h>#include <iostream>using namespace std;typedef struct node{int val;struct node *next;}node,*pNode;pNode CreateLinkList(pNode &T);pNode FindLastKth(pNode T,int k);#endifLinkList.cpp#include "LinkList.h"pNode CreateLinkList(pNode &T){int ival;cin>>ival;if(ival==0)return NULL;T = (pNode)malloc(sizeof(node));T->val = ival;T->next = NULL;T->next = CreateLinkList(T->next);return T;}pNode FindLastKth(pNode T,int k){pNode low=T,fast = T;for(int i=0;i<k&&fast!=NULL;i++){fast = fast->next;}if(fast == NULL)return NULL;while(fast!=NULL){fast = fast->next;low = low->next;}return low;}
Main.cpp#include "LinkList.h"void main(){pNode T;T = NULL;CreateLinkList(T);int k=3;pNode pnode;pnode = FindLastKth(T,k);cout<<pnode->val;system("pause");}


[Programming question] enter a one-way linked list and output the last K nodes in the list.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.