Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.cpp
More file actions
Latest commit
36 lines (32 loc) · 842 Bytes
/
Copy path2.cpp
File metadata and controls
36 lines (32 loc) · 842 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
/*************************************************************************
> File Name: 2.cpp
> Author: Louis1992
> Mail: zhenchaogan@gmail.com
> Blog: http://gzc.github.io
> Created Time: Mon Nov 9 08:30:19 2015
************************************************************************/
#include<iostream>
usingnamespacestd;
structListNode {
int v;
ListNode *next;
ListNode(int _v):v(_v){}
};
ListNode* kthtoLast(ListNode *head, int k) {
ListNode *fast(head);
while(k--) fast = fast->next;
while(fast) {
head = head->next;
fast = fast->next;
}
return head;
}
intmain() {
ListNode *l1 = newListNode(1);
ListNode *l2 = newListNode(2);
ListNode *l3 = newListNode(3);
l1->next = l2;
l2->next = l3;
cout << (kthtoLast(l1, 2))->v << endl;
return0;
}