-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-19-Remove-Nth-Node-From-End-of-List.java
More file actions
103 lines (83 loc) · 2.7 KB
/
LeetCode-19-Remove-Nth-Node-From-End-of-List.java
File metadata and controls
103 lines (83 loc) · 2.7 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
/*
LeetCode: https://leetcode.com/problems/remove-nth-node-from-end-of-list/
LintCode: http://www.lintcode.com/problem/remove-nth-node-from-end-of-list/
JiuZhang: http://www.jiuzhang.com/solutions/remove-nth-node-from-end-of-list/
ProgramCreek: http://www.programcreek.com/2014/05/leetcode-remove-nth-node-from-end-of-list-java/
Analysis:
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
// 1.Fast-Slow Pointers
// public ListNode removeNthFromEnd(ListNode head, int n) {
// if(head == null) return null;
// ListNode fast = head, slow = head;
// for(int i = 0; i < n; i++){
// fast = fast.next;
// }
// if(fast == null) return slow.next;
// while(fast.next != null){
// fast = fast.next;
// slow = slow.next;
// }
// slow.next = slow.next.next;
// return head;
// }
// 2.Count to find the Nth Pointer
// public ListNode removeNthFromEnd(ListNode head, int n) {
// if(head == null) return head;
// // get length of list
// int len = 0;
// ListNode curr = head;
// while(curr != null){
// len++;
// curr = curr.next;
// }
// // if head is nth, remove head
// int start = len - n + 1; // the pointer of the destination point
// if(start < 0) return null;
// if(start == 1) return head.next;
// // remove non-first node
// curr = head;
// int i = 0;
// while(curr != null){
// i++;
// // if i is the prev pointer of the destination point
// if(i == start - 1){
// curr.next = curr.next.next;
// }
// curr = curr.next;
// }
// return head;
// }
// 3.
public ListNode removeNthFromEnd(ListNode head, int n) {
if (n == 0) {
head = head.next;
return head;
}
int len = 0;
ListNode p = head;
while (p != null) {
len++;
p = p.next;
}
int move = len - n; // for instance, [1,2,3,4,5], 2, the move is 5-2=3
if (move == 0) return head.next;
p = head; // move p to the node precede to the destination node.
for (int i = 1; i < move; i++) {
p = p.next;
}
if (p.next == null) return head;
ListNode temp = p.next;
p.next = p.next.next;
temp.next = null;
return head;
}
}