Skip to content

Commit b705253

Browse files
committed
leetcode
1 parent 60467d4 commit b705253

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

PartitionList/partition_list.dart

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
3+
-* 86. Partition List *-
4+
5+
6+
Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
7+
8+
You should preserve the original relative order of the nodes in each of the two partitions.
9+
10+
11+
12+
Example 1:
13+
14+
15+
Input: head = [1,4,3,2,5,2], x = 3
16+
Output: [1,2,2,4,3,5]
17+
Example 2:
18+
19+
Input: head = [2,1], x = 2
20+
Output: [1,2]
21+
22+
23+
Constraints:
24+
25+
The number of nodes in the list is in the range [0, 200].
26+
-100 <= Node.val <= 100
27+
-200 <= x <= 200
28+
29+
30+
31+
*/
32+
33+
// Definition for singly-linked list.
34+
class ListNode {
35+
int val;
36+
ListNode? next;
37+
ListNode([this.val = 0, this.next]);
38+
}
39+
40+
class Solution {
41+
ListNode? partition(ListNode? head, int x) {
42+
ListNode left = ListNode(0);
43+
ListNode right = ListNode(0);
44+
ListNode temp1 = left;
45+
ListNode temp2 = right;
46+
47+
while (head != null) {
48+
if (head.val < x) {
49+
left.next = head;
50+
left = left.next!;
51+
} else {
52+
right.next = head;
53+
right = right.next!;
54+
}
55+
head = head.next;
56+
}
57+
58+
left.next = temp2.next;
59+
right.next = null;
60+
61+
return temp1.next;
62+
}
63+
}
64+
65+
class MERGESort {
66+
ListNode? merge(ListNode? firstList, ListNode? secondList, int x) {
67+
if (firstList == null) {
68+
return secondList;
69+
}
70+
if (secondList == null) {
71+
return firstList;
72+
}
73+
74+
ListNode newHead = ListNode(-9);
75+
ListNode temp = newHead;
76+
77+
while (firstList != null && secondList != null) {
78+
if (firstList.val < x) {
79+
temp.next = firstList;
80+
firstList = firstList.next;
81+
} else if (secondList.val < x) {
82+
temp.next = secondList;
83+
secondList = secondList.next;
84+
} else {
85+
break;
86+
}
87+
temp = temp.next!;
88+
}
89+
90+
if (firstList != null) {
91+
temp.next = firstList;
92+
while (temp.next != null) {
93+
temp = temp.next!;
94+
}
95+
}
96+
97+
if (secondList != null) {
98+
temp.next = secondList;
99+
}
100+
101+
return newHead.next;
102+
}
103+
104+
ListNode? partition(ListNode? head, int x) {
105+
if (head == null || head.next == null) {
106+
return head;
107+
}
108+
109+
ListNode? temp = head;
110+
ListNode? slow = head.next;
111+
ListNode? fast = slow?.next;
112+
113+
while (fast != null && fast.next != null) {
114+
temp = slow;
115+
slow = slow?.next;
116+
fast = fast.next?.next;
117+
}
118+
119+
temp?.next = null;
120+
121+
return merge(partition(head, x), partition(slow, x), x);
122+
}
123+
}

PartitionList/partition_list.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
func merge(firstList *ListNode, secondList *ListNode, x int) *ListNode {
9+
if firstList == nil {
10+
return secondList
11+
}
12+
if secondList == nil {
13+
return firstList
14+
}
15+
16+
newHead := &ListNode{Val: -9}
17+
temp := newHead
18+
19+
for firstList != nil && secondList != nil {
20+
if firstList.Val < x {
21+
temp.Next = firstList
22+
firstList = firstList.Next
23+
} else if secondList.Val < x {
24+
temp.Next = secondList
25+
secondList = secondList.Next
26+
} else {
27+
break
28+
}
29+
temp = temp.Next
30+
}
31+
32+
if firstList != nil {
33+
temp.Next = firstList
34+
for temp.Next != nil {
35+
temp = temp.Next
36+
}
37+
}
38+
39+
if secondList != nil {
40+
temp.Next = secondList
41+
}
42+
43+
return newHead.Next
44+
}
45+
46+
func partition(head *ListNode, x int) *ListNode {
47+
if head == nil || head.Next == nil {
48+
return head
49+
}
50+
51+
temp := head
52+
slow := head.Next
53+
fast := slow.Next
54+
55+
for fast != nil && fast.Next != nil {
56+
temp = slow
57+
slow = slow.Next
58+
fast = fast.Next.Next
59+
}
60+
61+
temp.Next = nil
62+
63+
return merge(partition(head, x), partition(slow, x), x)
64+
}

PartitionList/partition_list.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# DOC
2+
3+
[GO](https://leetcode.com/problems/partition-list/solutions/3911577/go-merge-sort-simple-fast-and-easy-with-explanation/)
4+
5+
[DART](https://leetcode.com/problems/partition-list/solutions/3911626/binary-tree-merge-sort-simple-fast-and-easy-with-explanation/)

0 commit comments

Comments
 (0)