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

Commit 58f9aaf

Browse files
authored
Added tasks 147-148.
1 parent 434e6ee commit 58f9aaf

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0101_0200.s0147_insertion_sort_list;
2+
3+
import com_github_leetcode.ListNode;
4+
import java.util.Arrays;
5+
6+
/*
7+
* Definition for singly-linked list.
8+
* public class ListNode {
9+
* int val;
10+
* ListNode next;
11+
* ListNode() {}
12+
* ListNode(int val) { this.val = val; }
13+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
14+
* }
15+
*/
16+
public class Solution {
17+
public ListNode insertionSortList(ListNode head) {
18+
ListNode tnode = head;
19+
ListNode res = null;
20+
int count = 0;
21+
while (tnode != null) {
22+
count++;
23+
tnode = tnode.next;
24+
}
25+
int[] nums = new int[count];
26+
for (int i = 0; i < count; i++) {
27+
nums[i] = head.val;
28+
head = head.next;
29+
}
30+
Arrays.sort(nums);
31+
for (int i = nums.length - 1; i >= 0; i--) {
32+
ListNode temp = new ListNode();
33+
temp.val = nums[i];
34+
temp.next = res;
35+
res = temp;
36+
}
37+
return res;
38+
}
39+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g0101_0200.s0148_sort_list;
2+
3+
import com_github_leetcode.ListNode;
4+
5+
/*
6+
* Definition for singly-linked list.
7+
* public class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode() {}
11+
* ListNode(int val) { this.val = val; }
12+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
13+
* }
14+
*/
15+
@SuppressWarnings("java:S135")
16+
public class Solution {
17+
public ListNode sortList(ListNode head) {
18+
if (head == null || head.next == null) {
19+
return head;
20+
}
21+
ListNode slow = head;
22+
ListNode fast = head;
23+
ListNode pre = slow;
24+
while (fast != null && fast.next != null) {
25+
pre = slow;
26+
slow = slow.next;
27+
fast = fast.next.next;
28+
}
29+
pre.next = null;
30+
ListNode first = sortList(head);
31+
ListNode second = sortList(slow);
32+
ListNode res = new ListNode(1);
33+
ListNode cur = res;
34+
while (first != null || second != null) {
35+
if (first == null) {
36+
cur.next = second;
37+
break;
38+
} else if (second == null) {
39+
cur.next = first;
40+
break;
41+
} else if (first.val <= second.val) {
42+
cur.next = first;
43+
first = first.next;
44+
cur = cur.next;
45+
} else {
46+
cur.next = second;
47+
second = second.next;
48+
cur = cur.next;
49+
}
50+
}
51+
return res.next;
52+
}
53+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0101_0200.s0147_insertion_sort_list;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.ListNode;
7+
import org.junit.Test;
8+
9+
public class SolutionTest {
10+
@Test
11+
public void insertionSortList() {
12+
ListNode listNode1 = new ListNode(4);
13+
listNode1.next = new ListNode(2);
14+
listNode1.next.next = new ListNode(1);
15+
listNode1.next.next.next = new ListNode(3);
16+
assertThat(new Solution().insertionSortList(listNode1).toString(), equalTo("1, 2, 3, 4"));
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0101_0200.s0148_sort_list;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.ListNode;
7+
import org.junit.Test;
8+
9+
public class SolutionTest {
10+
@Test
11+
public void sortList() {
12+
ListNode listNode1 = new ListNode(4);
13+
listNode1.next = new ListNode(2);
14+
listNode1.next.next = new ListNode(1);
15+
listNode1.next.next.next = new ListNode(3);
16+
assertThat(new Solution().sortList(listNode1).toString(), equalTo("1, 2, 3, 4"));
17+
}
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /