The last k node in the linked list

Source: Internet
Author: User


Idea: set two pointers, a slow pointer pSlow, a fast pointer pFast, fast pointer first K-1 step, then two pointers at the same time, when the fast pointer to the end of the chain table, the slow Pointer Points to the nearest k node.

Special case: If k is greater than the length of the linked list, the header node is returned.

Code:

# Include "stdafx. h "# include <iostream> using namespace std; struct ListNode {int m_nValue; ListNode * m_pNext ;}; ListNode * FindKthToTail (ListNode * pHead, int k) {if (pHead = NULL | k = 0) {return NULL;} ListNode * pSlow = pHead; ListNode * pFast = pHead; int I = 1; while (I <k) {pFast = pFast-> m_pNext; if (pFast = NULL) {cout <"k =" <k <"greater than the length of the linked list! "<Endl; break;} I ++;} if (pFast! = NULL) {while (pFast-> m_pNext! = NULL) {pSlow = pSlow-> m_pNext; pFast = pFast-> m_pNext;} return pSlow;} // create a linked list and enter the value of the start and end nodes, input-1 to end void CreateList (ListNode * & pHead) {ListNode * pListNode = NULL; ListNode * pCurLastNode = NULL; bool isHead = true; while (1) {if (isHead) {pHead = new ListNode (); cin> pHead-> m_nValue; pHead-> m_pNext = NULL; isHead = false; pCurLastNode = pHead ;} else {pListNode = new ListNode (); cin> PListNode-> m_nValue; if (pListNode-> m_nValue =-1) {break;} pListNode-> m_pNext = NULL; pCurLastNode-> m_pNext = pListNode; pCurLastNode = pListNode ;}}// print the void PrintList (ListNode * & pHead) {if (pHead! = NULL) {ListNode * pCur = pHead; while (pCur! = NULL) {cout <pCur-> m_nValue <""; pCur = pCur-> m_pNext;} cout <endl;} else {cout <"linked list is empty! "<Endl ;}} int _ tmain (int argc, _ TCHAR * argv []) {ListNode * pListHead = NULL; CreateList (pListHead); PrintList (pListHead ); listNode * pKthNodeToTail = FindKthToTail (pListHead, 6); cout <pKthNodeToTail-> m_nValue <endl; return 0 ;}# include "stdafx. h "# include <iostream> using namespace std; struct ListNode {int m_nValue; ListNode * m_pNext;}; ListNode * FindKthToTail (ListNode * pHead, int k) {if (pHead = NULL | k = 0) {return NULL;} ListNode * pSlow = pHead; ListNode * pFast = pHead; int I = 1; while (I <k) {pFast = pFast-> m_pNext; if (pFast = NULL) {cout <"k =" <k <"greater than the length of the linked list! "<Endl; break;} I ++;} if (pFast! = NULL) {while (pFast-> m_pNext! = NULL) {pSlow = pSlow-> m_pNext; pFast = pFast-> m_pNext;} return pSlow;} // create a linked list and enter the value of the start and end nodes, input-1 to end void CreateList (ListNode * & pHead) {ListNode * pListNode = NULL; ListNode * pCurLastNode = NULL; bool isHead = true; while (1) {if (isHead) {pHead = new ListNode (); cin> pHead-> m_nValue; pHead-> m_pNext = NULL; isHead = false; pCurLastNode = pHead ;} else {pListNode = new ListNode (); cin> pListNode-> m_nValue; if (pL IstNode-> m_nValue =-1) {break;} pListNode-> m_pNext = NULL; pCurLastNode-> m_pNext = pListNode; pCurLastNode = pListNode ;}}} // print the linked list void PrintList (ListNode * & pHead) {if (pHead! = NULL) {ListNode * pCur = pHead; while (pCur! = NULL) {cout <pCur-> m_nValue <""; pCur = pCur-> m_pNext;} cout <endl;} else {cout <"linked list is empty! "<Endl ;}} int _ tmain (int argc, _ TCHAR * argv []) {ListNode * pListHead = NULL; CreateList (pListHead); PrintList (pListHead ); listNode * pKthNodeToTail = FindKthToTail (pListHead, 6); cout <pKthNodeToTail-> m_nValue <endl; return 0 ;}

 

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.