-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path019_RemoveNthFromEnd.c
63 lines (56 loc) · 1.22 KB
/
019_RemoveNthFromEnd.c
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
//3.41%
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
//Check boundary conditions
if(!head || 0 >= n)
return head;
struct ListNode *ptr1 = head;
struct ListNode *ptr2 = head;
//Find the nth from the start
while(n--)
{
ptr2 = ptr2 -> next;
if(!ptr2)
{
if(0 == n)
head = head -> next;
return head;
}
}
//Untile the last endpoint
while(ptr2 -> next)
{
ptr1 = ptr1 -> next;
ptr2 = ptr2 -> next;
}
//Remove the nth endpoint from end, it's 'ptr1 -> next'
ptr1 -> next = ptr1 -> next -> next;
return head;
}
//3.41%
int removeNthFromEndHelper(struct ListNode *prev, int n)
{
struct ListNode *curr = prev -> next;
if(!curr)
return 0;
int pos = 1 + removeNthFromEndHelper(curr, n);
if(pos == n)
prev -> next = curr -> next;
return pos;
}
struct ListNode* removeNthFromEnd001(struct ListNode* head, int n) {
struct ListNode *prev = head;
int index = 0;
if(!prev || 0 >= n)
return head;
index = 1 + removeNthFromEndHelper(prev, n);
if(index == n)
head = head -> next;
return head;
}