Skip to content

Commit e85418a

Browse files
committed
feat: add ts solution to lcci problem: No.02.04
No.02.04.Partition List
1 parent 5d0a56f commit e85418a

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

lcci/02.04.Partition List/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,38 @@ public:
123123
};
124124
```
125125
126+
### **TypeScript**
127+
128+
```ts
129+
/**
130+
* Definition for singly-linked list.
131+
* class ListNode {
132+
* val: number
133+
* next: ListNode | null
134+
* constructor(val?: number, next?: ListNode | null) {
135+
* this.val = (val===undefined ? 0 : val)
136+
* this.next = (next===undefined ? null : next)
137+
* }
138+
* }
139+
*/
140+
141+
function partition(head: ListNode | null, x: number): ListNode | null {
142+
if (head == null) {
143+
return head;
144+
}
145+
let cur = head;
146+
while (cur.next != null) {
147+
let node = cur.next;
148+
if (node.val < x) {
149+
[head, node.next, cur.next] = [node, head, node.next];
150+
} else {
151+
cur = cur.next;
152+
}
153+
}
154+
return head;
155+
}
156+
```
157+
126158
### **...**
127159

128160
```

lcci/02.04.Partition List/README_EN.md

+32
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,38 @@ public:
118118
};
119119
```
120120
121+
### **TypeScript**
122+
123+
```ts
124+
/**
125+
* Definition for singly-linked list.
126+
* class ListNode {
127+
* val: number
128+
* next: ListNode | null
129+
* constructor(val?: number, next?: ListNode | null) {
130+
* this.val = (val===undefined ? 0 : val)
131+
* this.next = (next===undefined ? null : next)
132+
* }
133+
* }
134+
*/
135+
136+
function partition(head: ListNode | null, x: number): ListNode | null {
137+
if (head == null) {
138+
return head;
139+
}
140+
let cur = head;
141+
while (cur.next != null) {
142+
let node = cur.next;
143+
if (node.val < x) {
144+
[head, node.next, cur.next] = [node, head, node.next];
145+
} else {
146+
cur = cur.next;
147+
}
148+
}
149+
return head;
150+
}
151+
```
152+
121153
### **...**
122154

123155
```

lcci/02.04.Partition List/Solution.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function partition(head: ListNode | null, x: number): ListNode | null {
14+
if (head == null) {
15+
return head;
16+
}
17+
let cur = head;
18+
while (cur.next != null) {
19+
let node = cur.next;
20+
if (node.val < x) {
21+
[head, node.next, cur.next] = [node, head, node.next];
22+
} else {
23+
cur = cur.next;
24+
}
25+
}
26+
return head;
27+
}

0 commit comments

Comments
 (0)