forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_19.java
108 lines (95 loc) · 3.51 KB
/
_19.java
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
107
108
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
import com.fishercoder.common.utils.CommonUtils;
/**19. Remove Nth Node From End of List
*
* 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.
*/
public class _19 {
public static class Solution1 {
/**
* Naive/most straightforward approach:
* go through the list, find its total length, then go through the list a second time:
* this time, pause at the delta point, then assign its next.next pointer to next.
* This approach has to traverse the list twice, not one-pass.
*/
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode temp = head;
int len = 0;
while (temp != null) {
temp = temp.next;
len++;
}
if (n == len) {
return head.next;
}
temp = head;
int cut = len - n;
while (cut-- > 1) {
temp = temp.next;
}
if (temp.next != null) {
temp.next = temp.next.next;
return head;
}
return null;
}
}
public static class Solution2 {
public ListNode removeNthFromEnd(ListNode head, int n) {
//this approach uses two pointers, fast moves first for n nodes, when fast reaches n, then we start to move slow
//then, when fast reaches null, slow reaches the point where the node should be deleted.
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode slow = head;
ListNode fast = head;
int tempN = n;
while (tempN-- > 0) {
fast = fast.next;
}
if (fast == null) {
if (n > 0) {
// this is for cases like this: [1,2] 2 or [1,2,3,4] 4, namely, remove the head of
// the list and return the second node from the original list
dummy.next = dummy.next.next;
}
return dummy.next;
}
fast = fast.next;//we'll have to move fast pointer one node forward before moving the two together, this way,
//when fast reaches null, slow will be at the previous node to the node that should be deleted, thus, we can change the next pointer easily
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
if (slow.next != null) {
slow.next = slow.next.next;
}
return dummy.next;
}
}
public static class Solution3 {
//a more concise version using the same idea
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode slow = dummy;
ListNode fast = dummy;
while (fast.next != null) {
if (n <= 0) {
slow = slow.next;
}
fast = fast.next;
n--;
}
if (slow.next != null) {
slow.next = slow.next.next;
}
return dummy.next;
}
}
}