-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy path2-2.cpp
More file actions
43 lines (35 loc) · 693 Bytes
/
Copy path2-2.cpp
File metadata and controls
43 lines (35 loc) · 693 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
#include <iostream>
using namespace std;
class ListNode{
public:
int val;
ListNode* next;
ListNode(int i): val(i), next(NULL){}
};
int findlastn(ListNode* head, int n){
ListNode* p=head;
while(n>0 && p){
p=p->next;
n--;
}
if (n>0) {return 0;}
ListNode* q = head;
while (p->next){
q=q->next;
p=p->next;
}
return q->val;
}
int main(){
ListNode* head = new ListNode(1);
ListNode* q=head;
int a[15] = {1,2,3,4,5,6,7,4,5,2,10,8,9,0,5};
//int a[15] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
for (int i=0;i<15;i++){
ListNode* p=new ListNode(a[i]);
q->next = p;
q=p;
}
int n = 5;
cout << "The " << n << "th to last element is " << findlastn(head,n-1) << endl;
}