-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19.cpp
More file actions
106 lines (100 loc) · 2.36 KB
/
19.cpp
File metadata and controls
106 lines (100 loc) · 2.36 KB
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
100
101
102
103
104
105
106
/*
* 19.cpp
*
* Created on: Feb 28, 2015
* Author: zhangzuowei
*/
#include<iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
if(n < 1 || head == NULL)
return NULL;
ListNode *start = new ListNode(0), *pre = start;
pre -> next = head;
while(n--)
head = head -> next;
while(head) {
head = head -> next;
pre = pre -> next;
}
ListNode *target = pre -> next;
pre -> next = target -> next;
delete target;
return start -> next;
}
ListNode *removeNthFromEndWithoutHead(ListNode *head, int n) {
if(n < 1 || head == NULL)
return NULL;
ListNode *pre = head, *target = head -> next, *tail = target;
int i;
for(i = 0; i < n && tail != NULL; i++) {
tail = tail -> next;
}
if(i < n - 1)
return head;
else if(i == n - 1) {
head = head -> next;
delete pre;
return head;
}
while(tail != NULL) {
tail = tail -> next;
pre = target;
target = target -> next;
}
if(target -> next == NULL) {
delete target;
pre -> next = NULL;
return head;
}
pre -> next = target -> next;
delete target;
return head;
}
};
class Test {
public:
void sample() {
int input1[] = {1, 2, 3}, loc1 = 3, length1 = 3;
int input2[] = {1}, loc2 = 1, length2 = 1;
int input3[] = {1, 2}, loc3 = 2, length3 = 2;
int input4[] = {1, 2, 3}, loc4 = 1, length4 = 3;
runTest(input1, loc1, length1);
runTest(input2, loc2, length2);
runTest(input3, loc3, length3);
runTest(input4, loc4, length4);
}
private:
static ListNode *arrayToList(int input[], int length) {
if(length < 1)
return NULL;
ListNode *head = new ListNode(input[0]), *tail = head;
for(int i = 1; i < length; i++) {
ListNode *current = new ListNode(input[i]);
tail -> next = current;
tail = tail -> next;
}
return head;
}
static void printListNode(ListNode *head) {
while(head != NULL) {
cout << head -> val << "->";
head = head -> next;
}
cout << "NULL" <<endl;
}
static void runTest(int input[], int loc, int length) {
ListNode *l = arrayToList(input, length);
printListNode(l);
Solution *s = new Solution();
ListNode *result = s -> removeNthFromEnd(l ,loc);
printListNode(result);
}
};