Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Swift implementation for LCCI 02.04 #2617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lcci/02.04.Partition List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,44 @@ function partition(head: ListNode | null, x: number): ListNode | null {
}
```

```swift
/** public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ x: Int) {
* self.val = x
* self.next = nil
* }
* }
*/

class Solution {
func partition(_ head: ListNode?, _ x: Int) -> ListNode? {
let leftDummy = ListNode(0)
let rightDummy = ListNode(0)
var left = leftDummy
var right = rightDummy
var head = head

while let current = head {
if current.val < x {
left.next = current
left = left.next!
} else {
right.next = current
right = right.next!
}
head = head?.next
}

right.next = nil
left.next = rightDummy.next

return leftDummy.next
}
}
```

<!-- tabs:end -->

<!-- end -->
38 changes: 38 additions & 0 deletions lcci/02.04.Partition List/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,44 @@ function partition(head: ListNode | null, x: number): ListNode | null {
}
```

```swift
/** public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ x: Int) {
* self.val = x
* self.next = nil
* }
* }
*/

class Solution {
func partition(_ head: ListNode?, _ x: Int) -> ListNode? {
let leftDummy = ListNode(0)
let rightDummy = ListNode(0)
var left = leftDummy
var right = rightDummy
var head = head

while let current = head {
if current.val < x {
left.next = current
left = left.next!
} else {
right.next = current
right = right.next!
}
head = head?.next
}

right.next = nil
left.next = rightDummy.next

return leftDummy.next
}
}
```

<!-- tabs:end -->

<!-- end -->
35 changes: 35 additions & 0 deletions lcci/02.04.Partition List/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ x: Int) {
* self.val = x
* self.next = nil
* }
* }
*/

class Solution {
func partition(_ head: ListNode?, _ x: Int) -> ListNode? {
let leftDummy = ListNode(0)
let rightDummy = ListNode(0)
var left = leftDummy
var right = rightDummy
var head = head

while let current = head {
if current.val < x {
left.next = current
left = left.next!
} else {
right.next = current
right = right.next!
}
head = head?.next
}

right.next = nil
left.next = rightDummy.next

return leftDummy.next
}
}