List of the last K nodes in a list

Source: Internet
Author: User

Title: Enter a list, output the list of the last K nodes, in order to conform to the habit of most people, the topic starts from 1, that is, the tail node of the list is the penultimate node. For example, there is a list of 6 and nodes, starting from the beginning their values are 1,2,3,4,5,6. The 3rd node in the list is a node with a value of 4.

The linked list is defined as follows:

struct listnode{int m_nvalue; Listnode* M_pnext;};

Conventional ideas: traverse the list two times, the first time to count the number of linked list nodes, the second time can find the bottom of the K-node.

But there are ways that we just need to iterate through the list. As before, we can set two pointers. The first pointer is traversed from the head of the list to the forward k-1, and the second pointer remains motionless. Starting with step K, the second pointer is also traversed from the head pointer of the linked list. Since the distance of the two pointers is maintained at K-1, when the pointer to the front reaches the end of the list, the pointer at the back points exactly to the K-count node.

We also need to consider the robustness of the program: 1. If you enter a null pointer, the program crashes 2. If the total number of nodes in the list is less than K, we will attempt to access the null pointer and the program crashes. 3. The input k is 0, then the k-1 will be 0xFFFFFFFF, so the loop will be executed much more than we expected, which will also cause the program to crash.

Based on this analysis, the implementation code is as follows:

Listnode* findkthtotail (listnode* plisthead,unsigned int k) {    if ( plisthead==null| | k==0)         return NULL;         ListNode *pAhead=pListHead;    ListNode *pBehind=NULL;         for (unsigned int i=0;i<k-1;i++)     {         if (pahead->m_pnext!=null)              pAhead=pAhead->m_pNext;         else        {             return NULL;        }     }        pbehind=plisthead;    while ( P-ahead->m_pnext!=nuLL)     {        pAhead=pAhead->m_pNext;         pBehind=pBehind->m_pNext;    }         return pbehind;}

Extension: When we use a pointer to traverse the list does not solve the problem, you can try to use two pointers to traverse the list. You can make a pointer traverse faster, such as two steps on a linked list at a time, or let it take a few steps on the list first. Similar problems can be solved: to find the middle node of the list, to determine whether a one-way linked list has formed a circular structure and so on.


This article from "Fairy Road thousands of dust Dream" blog, please be sure to keep this source http://secondscript.blog.51cto.com/9370042/1583108

List of the last K nodes in a 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.