forked from forging2012/JavaArithmetic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode19.java
More file actions
66 lines (47 loc) · 1.52 KB
/
LeetCode19.java
File metadata and controls
66 lines (47 loc) · 1.52 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
package LeetCode;
public class LeetCode19 {
// 19. Remove Nth Node From End of List
// https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
// 先记录链表总长度
// 需要对链表进行两次遍历
// 时间复杂度: O(n)
// 空间复杂度: O(1)
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
int length = 0;
for(ListNode cur = dummyHead.next; cur != null ; cur = cur.next)
length ++;
int k = length - n;
assert k >= 0;
ListNode cur = dummyHead;
for(int i = 0 ; i < k ; i ++)
cur = cur.next;
cur.next = cur.next.next;
return dummyHead.next;
}
public ListNode removeNthFromEnd2(ListNode head, int n) {
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode p = dummyHead;
ListNode q = dummyHead;
// q是虚拟头结点
for( int i = 0 ; i < n + 1 ; i ++ ){
assert q != null;
q = q.next;
}
while(q != null){
p = p.next;
q = q.next;
}
p.next = p.next.next;
return dummyHead.next;
}
public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5};
ListNode head = new ListNode(arr);
System.out.println(head);
head = (new LeetCode19()).removeNthFromEnd2(head, 3);
System.out.println(head);
}
}