Reverse a singly linked list.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
func reverseList(_ head: ListNode?) -> ListNode? { var stack = [Int](), p = head while let t = p { stack.append(t.val) p = t.next } p = head while let t = p { t.val = stack.popLast()! p = t.next } return head } |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼