-
Notifications
You must be signed in to change notification settings - Fork 13
/
RemoveNthFromEnd19.java
70 lines (62 loc) · 1.69 KB
/
RemoveNthFromEnd19.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
package Leetcode;
import java.util.Stack;
public class RemoveNthFromEnd19 {
/*Method 1: Compute the length of list, Time: O(N), Space:O(1)**/
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
ListNode cur = dummy;
int len = getLength(head);
for(int i = 1 ;i < len - n + 1; i++) {
cur = cur.next;
}
cur.next = cur.next.next;
return dummy.next;
}
private int getLength(ListNode head) {
int count = 0;
while(head != null) {
count ++;
head = head.next;
}
return count;
}
/** Method 2: Stack(push & pop) + dummy, Time: O(Len), Space:O(Len)**/
public ListNode removeNthFromEnd2(ListNode head, int n) {
Stack<ListNode> stack = new Stack();
ListNode dummy = new ListNode(0, head);
ListNode cur = dummy;
while(cur != null) {
stack.push(cur);
cur = cur.next;
}
for(int i =0 ;i< n; i++) {
stack.pop();
}
ListNode prev = stack.pop();
prev.next = prev.next.next;
return dummy.next;
}
/*Method 3: Two pointers, Time: O(N), Space:O(1)**/
public ListNode removeNthFromEnd3(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
ListNode fast = head;
ListNode slow = dummy;
for(int i=0; i< n; i++) {
fast = fast.next;
}
while (fast!=null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
//Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
}