https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/submissions/
func reverseList(_ head: ListNode?) -> ListNode? {
if head == nil || head?.next == nil {return head}
var finallNode : ListNode? = nil
var cur = head
while cur != nil {
//将cur的下一个节点暂存,以便后续恢复链表遍历
let tmpNode = cur?.next
//将当前节点的next指向finall
cur?.next = finallNode
//将finall指向新生成的cur.next链表
finallNode = cur
//重新回复cur原生链表,继续遍历
cur = tmpNode
}
return finallNode
}
网友评论