-
Notifications
You must be signed in to change notification settings - Fork 0
/
19-RemoveNthNodeFromEndOfList.cpp
99 lines (85 loc) · 1.75 KB
/
19-RemoveNthNodeFromEndOfList.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
#include <stdio.h>
#include <unistd.h>
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
void print_list(ListNode *list) {
for ( ; list != NULL; list = list->next) {
printf("%d->", list->val);
}
printf("NULL\n");
}
class Solution {
public:
int getlength(ListNode *list) {
if (list == NULL) {
return 0;
}
int count = 0;
for ( ; list != NULL; list = list->next) {
count++;
}
return count;
}
ListNode* removeNthFromEnd(ListNode* head, int n) {
if (head == NULL) {
return head;
}
int len = getlength(head);
if (len == n ) {
return head->next;
}
int step = len - n - 1;
ListNode *p = head;
for (int count = 0; p != NULL; p = p->next) {
if (count != step) {
count++;
} else {
break;
}
}
p->next = p->next->next;
return head;
}
};
int main() {
// 2 1 3
ListNode l1(2), l2(1), l3(3);
ListNode l4(4), l5(2), l6(1), l7(6), l8(6), l9(9), l10(10), l11(11), l12(12), l13(6);
ListNode n1(0), n2(0), n3(0), n4(1);
l1.next = &l2;
//l2.next = &l3;
l4.next = &l5;
l5.next = &l6;
l6.next = &l7;
l7.next = &l8;
l8.next = &l9;
l9.next = &l10;
l10.next = &l11;
l11.next = &l12;
l12.next = &l13;
print_list(&l1);
Solution s;
ListNode *result = s.removeNthFromEnd(&l1, 2);
print_list(result);
return 0;
}