Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb45ab1d

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

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
returnresult.next;
5858
}
5959
}
60+
61+
publicstaticclassSolution2 {
62+
ListNodetail =newListNode(0);
63+
ListNodenextSubList =newListNode(0);
64+
65+
publicListNodesortList(ListNodehead) {
66+
if (head ==null ||head.next ==null)
67+
returnhead;
68+
intn =getCount(head);
69+
ListNodestart =head;
70+
ListNodedummyHead =newListNode(0);
71+
for (intsize =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+
ListNodemid =split(start,size);
79+
merge(start,mid);
80+
start =nextSubList;
81+
}
82+
start =dummyHead.next;
83+
}
84+
returndummyHead.next;
85+
}
86+
87+
ListNodesplit(ListNodestart,intsize) {
88+
ListNodemidPrev =start;
89+
ListNodeend =start.next;
90+
//use fast and slow approach to find middle and end of second linked list
91+
for (intindex =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+
ListNodemid =midPrev.next;
100+
midPrev.next =null;
101+
nextSubList =end.next;
102+
end.next =null;
103+
// return the start of second linked list
104+
returnmid;
105+
}
106+
107+
voidmerge(ListNodelist1,ListNodelist2) {
108+
ListNodedummyHead =newListNode(0);
109+
ListNodenewTail =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+
intgetCount(ListNodehead) {
133+
intcnt =0;
134+
ListNodeptr =head;
135+
while (ptr !=null) {
136+
ptr =ptr.next;
137+
cnt++;
138+
}
139+
returncnt;
140+
}
141+
}
60142
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp