-
Notifications
You must be signed in to change notification settings - Fork 21
/
19. Remove Nth Node From End of List.java
executable file
·63 lines (53 loc) · 1.38 KB
/
19. Remove Nth Node From End of List.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
M
tags: Linked List, Two Pointers
time: O(n)
space: O(1)
#### Two Pointer
- 1 end pointer to define the window based n steps
- 1 pre pointer to track the node before the targeting node
- when end reaches null, remove nth node: link pre and head.next
```
/*
Given a linked list, remove the nth node from the end of list and return its head.
Note
The minimum number of nodes in list is n.
Example
Given linked list: 1->2->3->4->5->null, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5->null.
Challenge
O(n) time
Tags Expand
Two Pointers Linked List
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/*
Thoughts:
Find starting piont of the window, and keep moving forward, until candidate reach end.
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1), pre = new ListNode(0);
pre.next = head;
dummy = pre;
// Establish window
ListNode end = head; // end node
while (n--> 0 && end != null) end = end.next;
// move window
while (end != null) {
end = end.next;
pre = pre.next;
head = head.next;
}
// remove nth node
pre.next = head.next;
return dummy.next;
}
}
```