• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode24.两两交换链表中的节点|SwapNodesinPairs

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9892062.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

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

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

Given a linked list, swap every two adjacent nodes and return its head.

Example:

Given 1->2->3->4, you should return the list as 2->1->4->3.

Note:

  • Your algorithm should use only constant extra space.
  • You may not modify the values in the list's nodes, only nodes itself may be changed.

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

示例:

给定 1->2->3->4, 你应该返回 2->1->4->3.

说明:

  • 你的算法只能使用常数的额外空间。
  • 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

8ms

 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 swapPairs(_ head: ListNode?) -> ListNode? {
14         var current = head
15         
16         let result = head?.next ?? head
17         
18         while let next = current?.next {
19             let nextNext = next.next
20             next.next = current
21             current?.next = nextNext?.next ?? nextNext
22             current = nextNext
23         }
24         
25         return result
26     }
27 }

12ms

 1 class Solution {
 2     func swapPairs(_ head: ListNode?) -> ListNode? {
 3         var current = head
 4         var next = current?.next
 5         while next != nil {
 6             (current!.val, next!.val) = (next!.val, current!.val)
 7 
 8             current = current?.next?.next
 9             next = current?.next
10         }
11         return head
12     }
13 }

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 swapNodes(pre: ListNode?, first: ListNode) {
14     let temp = first.next?.next
15     if pre != nil {
16         pre?.next = first.next
17 
18     }
19     if first.next?.next != nil {
20         first.next?.next = first
21         first.next = temp
22     } else {
23         first.next?.next = first
24         first.next = nil
25     }
26 }
27 
28 func swapPairs(_ head: ListNode?) -> ListNode? {
29     var pre: ListNode? = nil
30     var result = head
31     if let root = head {
32         if root.next != nil {
33             result = root.next
34         }
35         var current: ListNode? = head
36         while(current?.next != nil) {
37             swapNodes(pre: pre, first: current!)
38             pre = current
39             current = current?.next
40         }
41     }
42 
43     return result
44 }
45 }

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 swapPairs(_ head: ListNode?) -> ListNode? {
14         guard let head = head else {
15             return nil
16         }
17 
18         var left: ListNode? = head
19         var right = head.next
20         let root = head.next
21         while left != nil {
22             let temp = right?.next
23             right?.next = left
24             left?.next = temp?.next ?? temp
25 
26             left = temp
27             right = temp?.next
28         }
29 
30         return root ?? head
31     }
32 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode91.解码方法|DecodeWays发布时间:2022-07-13
下一篇:
[Swift]LeetCode1146.快照数组|SnapshotArray发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap