日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Swift]LeetCode206. 反转链表 | Reverse Linked List

發布時間:2025/4/16 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Swift]LeetCode206. 反转链表 | Reverse Linked List 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/9745480.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL

Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?


反轉一個單鏈表。

示例:

輸入: 1->2->3->4->5->NULL 輸出: 5->4->3->2->1->NULL

進階:
你可以迭代或遞歸地反轉鏈表。你能否用兩種方法解決這道題?


20ms

1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func reverseList(_ head: ListNode?) -> ListNode? { 14 if head == nil || head?.next == nil 15 { 16 return head 17 } 18 var h = reverseList(head?.next) 19 head?.next?.next = head 20 head?.next = nil 21 return h 22 } 23 }

20ms

1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func reverseList(_ head: ListNode?) -> ListNode? { 14 if head == nil { 15 return nil 16 } 17 18 var pre : ListNode? 19 var next : ListNode? 20 var cur : ListNode? = head 21 22 while (cur != nil) { 23 next = cur?.next; 24 cur?.next = pre; 25 pre = cur; 26 cur = next; 27 } 28 29 return pre; 30 } 31 }

20ms

1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func reverseList(_ head: ListNode?) -> ListNode? { 14 if head == nil { 15 return head 16 } 17 18 if head?.next == nil { 19 return head 20 } 21 22 guard let r = reverseList(head?.next) else { return nil } 23 if r.next == nil { 24 r.next = head 25 } else { 26 head!.next!.next = head 27 } 28 head!.next = nil 29 return r 30 } 31 }

24ms

1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func reverseList(_ head: ListNode?) -> ListNode? { 14 if head == nil || head?.next == nil { 15 return head 16 } 17 18 guard let r = reverseList(head?.next) else { return nil } 19 if r.next == nil { 20 r.next = head 21 } else { 22 head!.next!.next = head 23 } 24 head!.next = nil 25 return r 26 } 27 }

24ms

1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func reverseList(_ head: ListNode?) -> ListNode? { 14 if head == nil { 15 return nil 16 } 17 var root = head 18 19 var stack = [ListNode]() 20 while root != nil { 21 stack.append(root!) 22 root = root!.next 23 } 24 25 root = stack.last! 26 var next = root 27 for i in stride(from: stack.count - 2, to: -1, by: -1) { 28 let node = stack[i] 29 node.next = nil 30 next?.next = node 31 next = node; 32 } 33 return root 34 } 35 }

?

轉載于:https://www.cnblogs.com/strengthen/p/9745480.html

總結

以上是生活随笔為你收集整理的[Swift]LeetCode206. 反转链表 | Reverse Linked List的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。