-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_kth_to_tail.cpp
More file actions
65 lines (51 loc) · 1.27 KB
/
find_kth_to_tail.cpp
File metadata and controls
65 lines (51 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* 常见题目
*
* 输入一个链表,输出该链表中倒数第k个结点。
*
*
*/
#include <list>
#include <iostream>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
int FindKthToTail(ListNode* pListHead, unsigned int k) {
int current_num = 0;
list<int> mylist;
while(pListHead) {
if (current_num >= k) {
int current_top = mylist.front();
mylist.pop_front();
cout<<"pop front "<<current_top<<endl;
}
mylist.push_back(pListHead->val);
current_num ++;
cout<<"current_num "<<current_num<<endl;
pListHead = pListHead->next;
}
cout<<"value "<<mylist.front()<<endl;
return mylist.front();
}
};
int main()
{
class Solution t;
struct ListNode *mylist;
struct ListNode *p;
p = mylist = new ListNode(1);
mylist->next = new ListNode(2);
mylist->next->next = new ListNode(3);
mylist->next->next->next = new ListNode(4);
mylist->next->next->next->next = new ListNode(5);
mylist->next->next->next->next->next = new ListNode(6);
t.FindKthToTail(p, 2);
return 0;
}