敢教日月换新天。为有牺牲多壮志,

[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 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }
复制代码

 

posted @   为敢技术  阅读(574)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°