-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkth_to_last.cpp
More file actions
52 lines (45 loc) · 927 Bytes
/
Copy pathkth_to_last.cpp
File metadata and controls
52 lines (45 loc) · 927 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
45
46
47
48
49
50
51
#include <stdio.h>
class Node {
int data;
Node *next;
public:
int getData() { return data; }
Node(int mydata, Node *nextNode) : data(mydata), next(nextNode) {}
~Node() {
if(next)
delete next;
}
Node *fromLastElt(unsigned int K) {
Node *kNode1 = this;
for(unsigned int i=0; i<K; ++i) {
kNode1 = kNode1->next;
if(!kNode1)
return NULL;
}
Node *kNode2 = this;
while(kNode1->next) {
kNode1 = kNode1->next;
kNode2 = kNode2->next;
}
return kNode2;
}
void print() {
printf("%d ", data);
if(next)
next->print();
}
};
int main() {
// static const unsigned int K=3;
static const unsigned int K=1;
Node *head = new Node(1, new Node(2, new Node(3, new Node(4, NULL))));
// Node *head = new Node(1, new Node(2, NULL));
head->print();
printf("\n");
Node *kTL = head->fromLastElt(K);
if(kTL)
printf("%d\n", kTL->getData());
else
printf("Not found\n");
delete head;
}