The K-th node in the linked list.

Source: Internet
Author: User

The K-th node in the linked list.
Question: enter a linked list and output the last K nodes in the list. To meet the habits of most people, this topic starts counting from 1, that is, the End Node of the linked list is the last 1st nodes. For example, a linked list has six nodes and their values start from the first node are 1, 2, 3, 4, 5, and 6 in sequence. The last 3rd nodes of the linked list are 4 nodes.

Idea: set two pointers p1 and p2, the two pointers are pointing to the first node of the linked list at the beginning, and then let the p1 pointer first (k-1) step, and then let the two pointers go back together, when the p1 Pointer Points to the last node of the linked list, the p2 Pointer Points to the last k node in the linked list.

When writing code, you need to consider robustness. it is best to adopt defensive programming, that is, to consider where errors will occur, and then add error judgment in advance, so as to avoid program crash caused by incorrect input.

1 # include <iostream> 2 # include <stdio. h> 3 # include <tchar. h> 4 using namespace std; 5 6 // linked list structure 7 struct ListNode 8 {9 int m_nValue; 10 ListNode * m_pNext; 11 }; 12 13 // create a linked list node 14 ListNode * CreateListNode (int value) 15 {16 ListNode * pNode = new ListNode (); 17 pNode-> m_nValue = value; 18 pNode-> m_pNext = NULL; 19 20 return pNode; 21} 22 23 // output the value of a node in the linked list 24 void PrintListNode (ListNode * pNode) 25 {26 if (pNode = NULL) 27 printf ("The node is NULL \ n"); 28 else 29 printf ("The key in node is % d. \ n ", pNode-> m_nValue); 30} 31 32 // output linked list 33 void PrintList (ListNode * pHead) 34 {35 ListNode * pNode = pHead; 36 while (pNode! = NULL) 37 {38 cout <pNode-> m_nValue <"; 39 pNode = pNode-> m_pNext; 40} 41 cout <endl; 42} 43 44 // delete node 45 void DestroyList (ListNode * pHead) 46 {47 ListNode * pNode = pHead; 48 while (pNode! = NULL) 49 {50 pHead = pHead-> m_pNext; 51 delete pNode; 52 pNode = pHead; 53} 54} 55 56 // Add node 57/* 58 to the end of the linked list. Note that pHead is a pointer to a pointer, which is generally passed as a reference in the main function. 59. If you want to add nodes to the linked list, the structure of the linked list will be modified. Therefore, you must pass a reference to save the modified structure. 60 */61 void AddToTail (ListNode ** pHead, int value) 62 {63 ListNode * pNew = new ListNode (); 64 pNew-> m_nValue = value; 65 pNew-> m_pNext = NULL; 66 67 if (* pHead = NULL) 68 {69 * pHead = pNew; 70} 71 else 72 {73 ListNode * pNode = * pHead; 74 while (pNode-> m_pNext! = NULL) 75 pNode = pNode-> m_pNext; 76 77 pNode-> m_pNext = pNew; 78} 79} 80 81 82 // link two nodes 83 // void ConnectListNodes (ListNode * pCurrent, ListNode * pNext) 84 // {85 // if (pCurrent = NULL) 86 // {87 // printf ("Error to connect two nodes. \ n "); 88 // exit (1); 89 //} 90 // pCurrent-> m_pNext = pNext; 91 //} 92 93 // Defensive Programming, better robustness 94 ListNode * FindKthToTail (ListNode * pListHead, unsigned int k) 95 {96 I F (pListHead = NULL | k = 0) 97 return NULL; 98 99 ListNode * pAhead = pListHead; 100 ListNode * pBehind = NULL; 101 102 for (unsigned int I = 0; I <k-1; I ++) 103 {104 if (pAhead-> m_pNext! = NULL) 105 pAhead = pAhead-> m_pNext; 106 else107 return NULL; 108} 109 110 pBehind = pListHead; 111 while (pAhead-> m_pNext! = NULL) 112 {113 pAhead = pAhead-> m_pNext; 114 pBehind = pBehind-> m_pNext; 115} 116 117 return pBehind; 118} 119 120 int main () 121 {122 // create node 123 ListNode * pNode1 = CreateListNode (1); // create a node 124 PrintList (pNode1 ); // print 125 // Add the new node 126 AddToTail (& pNode1, 2) to the linked list; // Add a node 127 AddToTail (& pNode1, 3) to the linked list ); // Add a node 128 AddToTail (& pNode1, 4) to the linked list; // Add a node 129 AddToTail (& pNode1, 5) to the linked list ); // Add a node 130 AddToTail (& pNode1, 6) to the linked list; // Add a node 131 AddToTail (& pNode1, 7) to the linked list ); // Add a node 132 to the linked list // print the linked list 133 PrintList (pNode1); // print 134 // reverse the linked list 135 ListNode * KthNode = FindKthToTail (pNode1, 3 ); 136 137 PrintListNode (KthNode); 138 139 DestroyList (pNode1); 140 141 return 0; 142}

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.