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 87d9b0b

Browse files
feat: add swift implementation to lcci problem: No.02.04 (#2617)
Signed-off-by: Lanre Adedara
1 parent 65338e5 commit 87d9b0b

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

‎lcci/02.04.Partition List/README.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,44 @@ function partition(head: ListNode | null, x: number): ListNode | null {
201201
}
202202
```
203203

204+
```swift
205+
/** public class ListNode {
206+
* var val: Int
207+
* var next: ListNode?
208+
* init(_ x: Int) {
209+
* self.val = x
210+
* self.next = nil
211+
* }
212+
* }
213+
*/
214+
215+
class Solution {
216+
func partition(_ head: ListNode?, _ x: Int) -> ListNode? {
217+
let leftDummy = ListNode(0)
218+
let rightDummy = ListNode(0)
219+
var left = leftDummy
220+
var right = rightDummy
221+
var head = head
222+
223+
while let current = head {
224+
if current.val < x {
225+
left.next = current
226+
left = left.next!
227+
} else {
228+
right.next = current
229+
right = right.next!
230+
}
231+
head = head?.next
232+
}
233+
234+
right.next = nil
235+
left.next = rightDummy.next
236+
237+
return leftDummy.next
238+
}
239+
}
240+
```
241+
204242
<!-- tabs:end -->
205243

206244
<!-- end -->

‎lcci/02.04.Partition List/README_EN.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,44 @@ function partition(head: ListNode | null, x: number): ListNode | null {
179179
}
180180
```
181181

182+
```swift
183+
/** public class ListNode {
184+
* var val: Int
185+
* var next: ListNode?
186+
* init(_ x: Int) {
187+
* self.val = x
188+
* self.next = nil
189+
* }
190+
* }
191+
*/
192+
193+
class Solution {
194+
func partition(_ head: ListNode?, _ x: Int) -> ListNode? {
195+
let leftDummy = ListNode(0)
196+
let rightDummy = ListNode(0)
197+
var left = leftDummy
198+
var right = rightDummy
199+
var head = head
200+
201+
while let current = head {
202+
if current.val < x {
203+
left.next = current
204+
left = left.next!
205+
} else {
206+
right.next = current
207+
right = right.next!
208+
}
209+
head = head?.next
210+
}
211+
212+
right.next = nil
213+
left.next = rightDummy.next
214+
215+
return leftDummy.next
216+
}
217+
}
218+
```
219+
182220
<!-- tabs:end -->
183221

184222
<!-- end -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/** public class ListNode {
2+
* var val: Int
3+
* var next: ListNode?
4+
* init(_ x: Int) {
5+
* self.val = x
6+
* self.next = nil
7+
* }
8+
* }
9+
*/
10+
11+
class Solution {
12+
func partition(_ head: ListNode?, _ x: Int) -> ListNode? {
13+
let leftDummy = ListNode(0)
14+
let rightDummy = ListNode(0)
15+
var left = leftDummy
16+
var right = rightDummy
17+
var head = head
18+
19+
while let current = head {
20+
if current.val < x {
21+
left.next = current
22+
left = left.next!
23+
} else {
24+
right.next = current
25+
right = right.next!
26+
}
27+
head = head?.next
28+
}
29+
30+
right.next = nil
31+
left.next = rightDummy.next
32+
33+
return leftDummy.next
34+
}
35+
}

0 commit comments

Comments
(0)

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