C + + algorithm in the list of the penultimate K nodes

Source: Internet
Author: User

Title: Enter a list, output the list of the countdown K nodes, in order to conform to the majority of the habit, the topic from 1 to count, that is, the tail node of the list is the penultimate node.

Method 1: The list is traversed first to get the number of the list n, the penultimate K node is n-k+1 and then traverse the list, find the N-k+1 node is the penultimate K node; This method traverses two linked lists;

Method 2: First traverse the list to press the linked list into a stack, then out of the stack, the K-time out of the stack is the K-node;

Method 3: First reverse the linked list, then traverse

Method 4: Define two pointers, the first pointer starts from the head pointer of the list to traverse forward k-1; the second pointer remains motionless, starting with step K, the second pointer also begins to traverse, two pointer gaps k-1 distance

, when the first pointer goes to the tail node, the second pointer is exactly at the bottom of the K-node;

Code:

listnode* Findkthfromtail (listnode* plisthead,int k) {if (Plisthead = NULL | | k = = 0)//Prevent empty linked list  and K value equals 0 case {return NUL L;} listnode* pahead = Plisthead; listnode* pbehind = null;for (int i = 0; i < k-1; i++) {if (Pahead->m_pnext! = NULL) {pahead = Pahead->m_pnext;} Else{return NULL;}} Pbehind = Plisthead;while (pahead->m_pnext! = NULL) {Pahead  = Pahead->m_pnext;pbehind = Pbehind->m_pnext;} return pbehind;}

All complete code:

FindKthFromTail.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace std;struct listnode {int m_nvalue; listnode* M_pnext; ListNode (int k): M_nvalue (k), M_pnext (NULL) {}}; listnode* Findkthfromtail (listnode* plisthead,int k) {if (Plisthead = = NULL | | k = = 0)//Prevent empty linked list and K value equal to 0 case {return NULL;} listnode* pahead = Plisthead; listnode* pbehind = null;for (int i = 0; i < k-1; i++) {if (Pahead->m_pnext! = NULL) {pahead = Pahead->m_pnext;} Else{return NULL;}} Pbehind = Plisthead;while (pahead->m_pnext! = NULL) {pahead = Pahead->m_pnext;pbehind = Pbehind->m_pnext;} return pbehind;} int _tmain (int argc, _tchar* argv[]) {listnode* head = new ListNode (1); listnode* Node1 = new ListNode (2); listnode* Node2 = new ListNode (3); listnode* Node3 = new ListNode (4); listnode* Node4 = new ListNode (5); listnode* NODE5 = new ListNode (6); listnode* Node6 = new ListNode (7); head->m_pnext = Node1; Node1->m_pnext = Node2; Node2->m_pnext = Node3; Node3->m_pnext = Node4; Node4->m_pnext = NODE5; Node5->m_pnext = Node6; Node6->m_pnext = NULL; listnode* p = findkthfromtail (head,3); Cout<<p->m_nvalue<<endl;getchar (); return 0;}

C + + algorithm in the list of the penultimate K nodes

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.