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

feat: add swift implementation to lcof2 problem: No.024 #3012

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
yanglbme merged 2 commits into doocs:main from klever34:swift-impl-lcof2-024
Jun 3, 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
32 changes: 32 additions & 0 deletions lcof2/剑指 Offer II 024. 反转链表/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,38 @@ public class Solution {
}
```

#### Swift

```swift
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
*/

class Solution {
func reverseList(_ head: ListNode?) -> ListNode? {
var prev: ListNode? = nil
var current = head

while current != nil {
let next = current?.next
current?.next = prev
prev = current
current = next
}

return prev
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
27 changes: 27 additions & 0 deletions lcof2/剑指 Offer II 024. 反转链表/Solution.swift
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
*/

class Solution {
func reverseList(_ head: ListNode?) -> ListNode? {
var prev: ListNode? = nil
var current = head

while current != nil {
let next = current?.next
current?.next = prev
prev = current
current = next
}

return prev
}
}

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