-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path14.cpp
More file actions
44 lines (40 loc) · 659 Bytes
/
14.cpp
File metadata and controls
44 lines (40 loc) · 659 Bytes
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
#include <iostream>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) :val(x), next(NULL) {}
ListNode() {}
};
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k)
{
ListNode *p, *q;
p = q = pListHead;
int i = 0;
for (; p != NULL; i++)
{
if (i >= k)
q = q->next;
p = p->next;
}
return i < k ? NULL : q;
}
int main()
{
ios::sync_with_stdio(false);
int n, k;
ListNode* L = new ListNode;
L->next = NULL;
cin >> n;
for (int i = 0; i < n; i++)
{
ListNode *p = new ListNode;
cin >> p->val;
p->next = L->next;
L->next = p;
}
cin >> k;
cout << FindKthToTail(L, k)->val << endl;
return 0;
}