forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution1.java
38 lines (29 loc) · 968 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
/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
//
// Time Complexity: O(n)
// Space Complexity: 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);
}
}