[Programmer interview question selection 100 questions] 9. The last k nodes in the linked list, programmer nodes
Question
Enter a one-way linked list and output the last k nodes in the list. The last 0th nodes of the linked list are the tail pointer of the linked list.
Train of Thought 1
Because it is a one-way linked list, there is only a pointer from the past to the next, instead of a pointer from the next. Therefore, we can only traverse tables in forward order rather than reverse order. Assuming that the entire linked list has n nodes, the last k nodes are the n-k-1 nodes starting from the first node (counting from 0 ). We only need to get the number n of nodes in the linked list, then we only need to start from the node Back To Go To The n-k-1 step.
Therefore, this method needs to be used to traverse the chain table twice. The number of nodes in the linked list is n for the first time, and the number of n-k-1 nodes starting from the first node is k for the second time. The time complexity is O (n ).
Code
/* ------------------------------------ * Date: 2015-02-08 * Author: SJF0115 * question: 9. the last k node in the linked list * Source: programmer interview questions featured 100 questions ------------------------------------- */# include <iostream> # include <cstring> # include <vector> # include <queue> using namespace std; struct ListNode {int val; listNode * next; ListNode (int x): val (x), next (NULL) {}}; class Solution {public: ListNode * FindKthTailNode (ListNode * head, int k) {if (head = nullptr | k <0) {return nullptr;} // if // count the number of linked lists int count = 0; ListNode * p = head; while (p) {p = p-> next; + + count;} // while // less than K if (count <k) {return nullptr ;} // if // The last K node int pos = count-k; p = head; for (int I = 0; I <pos; ++ I) {p = p-> next;} // for return p ;}}; int main () {Solution s; ListNode * head = new ListNode (1); ListNode * node; for (int I = 8; I> = 2; -- I) {node = new ListNode (I); node-> next = head-> next; head-> next = node;} // for ListNode * result = s. findKthTailNode (head, 3); // output if (result = nullptr) {cout <"nullptr" <endl ;} // if else {cout <result-> val <endl;} // else return 0 ;}
Train of Thought 2
The above IDEA requires two traversal times. How can we only traverse one time?
If we maintain two pointers over time, the first pointer traverses from the head pointer of the linked list, and the second pointer remains unchanged before the K-1 step, the K-1 step begins, the second pointer also starts to traverse from the head pointer of the linked list, and the two pointers go hand in hand. Since the distance between the two pointers is kept in the K-1; when the first (walking in front) pointer reaches the end of the linked list, the second pointer (walking behind) pointer is exactly the penultimate
K nodes.
Code 2
/* ------------------------------------ * Date: 2015-02-08 * Author: SJF0115 * question: 9. the last k node in the linked list * Source: programmer interview questions featured 100 questions ------------------------------------- */# include <iostream> # include <cstring> # include <vector> # include <queue> using namespace std; struct ListNode {int val; listNode * next; ListNode (int x): val (x), next (NULL) {}}; class Solution {public: ListNode * FindKthTailNode (ListNode * head, int k) {if (Head = nullptr | k <0) {return nullptr;} // if ListNode * p = head, * q = head; // pointer p move K-1 step int index = 1; while (index <k & p! = Nullptr) {p = p-> next; ++ index;} // while // if (p = nullptr) {return nullptr ;} // if // move while (p-> next) {p = p-> next; q = q-> next ;}// while return q ;}}; int main () {Solution s; ListNode * head = new ListNode (1); ListNode * node; for (int I = 8; I> = 2; -- I) {node = new ListNode (I); node-> next = head-> next; head-> next = node;} // for ListNode * result = s. findKthTailNode (head, 8); // output if (result = nullptr) {cout <"nullptr" <endl ;} // if else {cout <result-> val <endl;} // else return 0 ;}
Expansion
Enter a one-way linked list. If the number of knots in the linked list is an odd number, the intermediate node is output. If the number of knots in the linked list is an even number, one node before the two nodes is output.
Code
/* ---------------------------------- * Date: 2015-02-08 * Author: SJF0115 * question: 9.2 central node of the linked list * Source: programmer interview questions featured 100 questions ------------------------------------- */# include <iostream> # include <cstring> # include <vector> # include <queue> using namespace std; struct ListNode {int val; listNode * next; ListNode (int x): val (x), next (NULL) {}}; class Solution {public: ListNode * FindMidNode (ListNode * head) {if (head = nullp Tr) {return nullptr;} // if ListNode * slow = head, * fast = head; while (fast-> next! = Nullptr & fast-> next! = Nullptr) {slow = slow-> next; fast = fast-> next;} // while return slow ;}; int main () {Solution s; listNode * head = new ListNode (1); ListNode * node; for (int I = 8; I> = 2; -- I) {node = new ListNode (I ); node-> next = head-> next; head-> next = node;} // for ListNode * result = s. findMidNode (head); // output if (result = nullptr) {cout <"nullptr" <endl ;} // if else {cout <result-> val <endl;} // else return 0 ;}