-
Notifications
You must be signed in to change notification settings - Fork 0
/
206_reverse_listnode.swift
43 lines (43 loc) · 1.29 KB
/
206_reverse_listnode.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
// func reverseList(_ head: ListNode?) -> ListNode? {
// // Solution 1: iterative loop
// var head = head
// var newHead: ListNode? = nil
// while head != nil {
// let next = head?.next
// head?.next = newHead
// newHead = head
// head = next
// }
// return newHead
// }
func reverseList(_ head: ListNode?) -> ListNode? {
// Solution 2: recursion
if let h = head {
let (n, first) = returnNext(h)
n!.next = nil
return first
}
return head
}
func returnNext(_ prev: ListNode?) -> (ListNode?, ListNode?) { // return next/first
if let node = prev {
if let next = node.next {
var (n, first) = returnNext(next)
n!.next = node
return (node, first)
}
}
return (prev, prev)
}
}