-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday30.java
More file actions
70 lines (64 loc) · 1.83 KB
/
Copy pathday30.java
File metadata and controls
70 lines (64 loc) · 1.83 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
// Q1: https://leetcode.com/problems/sort-list/
class Solution {
public ListNode sortList(ListNode head) {
if(head==null||head.next==null) return head;
ListNode mid=middle(head);
ListNode right=mid.next;
mid.next=null;
ListNode leftsort=sortList(head);
ListNode rightsort=sortList(right);
return merge(leftsort,rightsort);
}
private ListNode middle(ListNode head) {
ListNode slow = head, fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
private ListNode merge(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode current = dummy;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
if (l1 != null) {
current.next = l1;
}
if (l2 != null) {
current.next = l2;
}
return dummy.next;
}
}
TC-O(nlogn)
SC-o(1)
// Q2: https://leetcode.com/problems/remove-nth-node-from-end-of-list/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head == null) return null;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode fast = dummy, slow = dummy;
for (int i = 0; i <= n; i++) {
if (fast == null) return head;
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}
// TC-O(N)
// SC-O(1)