链表反转
class Solution {
func reverseList(_ head:ListNode?) -> ListNode? {
var prevNode :ListNode? = nil
var reverseNode :ListNode? = nil
var node = head
while (node != nil) {
let nextNode = node?.next
if (nextNode == nil) {
reverseNode = node
}
// 反转node结点
node?.next = prevNode
// 保存上一个结点,
prevNode = node
node = nextNode
}
}
return reverseNode
}