-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathSolution1.java
39 lines (30 loc) · 1013 Bytes
/
Solution1.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
// 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 class Solution1 {
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 static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5};
ListNode head = new ListNode(arr);
System.out.println(head);
head = (new Solution1()).removeNthFromEnd(head, 2);
System.out.println(head);
}
}