Skip to content

Commit b45ab1d

Browse files
Ildar10nazmeevi
andauthored
added a O(1) memory solution for 148 sort list (fishercoder1534#163)
Co-authored-by: nazmeevi <ildar.nazmeev@tipico.com>
1 parent a7b1dfb commit b45ab1d

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ _If you like this project, please leave me a star._ &#9733;
10821082
|151|[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_151.java)| | Medium| String
10831083
|150|[Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_150.java)| |Medium
10841084
|149|[Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_149.java)| |Hard|
1085+
|148|[Sort List](https://leetcode.com/problems/sort-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_148.java) ||Medium| Linked List, Sorting
10851086
|147|[Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_147.java) ||Medium| Linked List
10861087
|146|[LRU Cache](https://leetcode.com/problems/lru-cache/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_146.java)| |Hard| Doubly Linked List, LinkedHashMap
10871088
|145|[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_145.java)|[:tv:](https://youtu.be/B6XTLPpsW7k) |Easy| Binary Tree

src/main/java/com/fishercoder/solutions/_148.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,86 @@ private ListNode merge(ListNode l1, ListNode l2) {
5757
return result.next;
5858
}
5959
}
60+
61+
public static class Solution2 {
62+
ListNode tail = new ListNode(0);
63+
ListNode nextSubList = new ListNode(0);
64+
65+
public ListNode sortList(ListNode head) {
66+
if (head == null || head.next == null)
67+
return head;
68+
int n = getCount(head);
69+
ListNode start = head;
70+
ListNode dummyHead = new ListNode(0);
71+
for (int size = 1; size < n; size = size * 2) {
72+
tail = dummyHead;
73+
while (start != null) {
74+
if (start.next == null) {
75+
tail.next = start;
76+
break;
77+
}
78+
ListNode mid = split(start, size);
79+
merge(start, mid);
80+
start = nextSubList;
81+
}
82+
start = dummyHead.next;
83+
}
84+
return dummyHead.next;
85+
}
86+
87+
ListNode split(ListNode start, int size) {
88+
ListNode midPrev = start;
89+
ListNode end = start.next;
90+
//use fast and slow approach to find middle and end of second linked list
91+
for (int index = 1; index < size && (midPrev.next != null || end.next != null); index++) {
92+
if (end.next != null) {
93+
end = (end.next.next != null) ? end.next.next : end.next;
94+
}
95+
if (midPrev.next != null) {
96+
midPrev = midPrev.next;
97+
}
98+
}
99+
ListNode mid = midPrev.next;
100+
midPrev.next = null;
101+
nextSubList = end.next;
102+
end.next = null;
103+
// return the start of second linked list
104+
return mid;
105+
}
106+
107+
void merge(ListNode list1, ListNode list2) {
108+
ListNode dummyHead = new ListNode(0);
109+
ListNode newTail = dummyHead;
110+
while (list1 != null && list2 != null) {
111+
if (list1.val < list2.val) {
112+
newTail.next = list1;
113+
list1 = list1.next;
114+
newTail = newTail.next;
115+
} else {
116+
newTail.next = list2;
117+
list2 = list2.next;
118+
newTail = newTail.next;
119+
}
120+
}
121+
newTail.next = (list1 != null) ? list1 : list2;
122+
// traverse till the end of merged list to get the newTail
123+
while (newTail.next != null) {
124+
newTail = newTail.next;
125+
}
126+
// link the old tail with the head of merged list
127+
tail.next = dummyHead.next;
128+
// update the old tail to the new tail of merged list
129+
tail = newTail;
130+
}
131+
132+
int getCount(ListNode head) {
133+
int cnt = 0;
134+
ListNode ptr = head;
135+
while (ptr != null) {
136+
ptr = ptr.next;
137+
cnt++;
138+
}
139+
return cnt;
140+
}
141+
}
60142
}

0 commit comments

Comments
 (0)