Skip to content
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
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
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
}
}