[Swift]LeetCode24. 兩兩交換鏈表中的節點 | Swap Nodes in Pairs

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-qittonnk-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

Example:git

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

Note:github

  • 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->42->1->4->3

說明:spa

  • 你的算法只能使用常數的額外空間。
  • 你不能只是單純的改變節點內部的值,而是須要實際的進行節點交換。

8mscode

 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 }

12mshtm

 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 }

20msblog

 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 }
相關文章
相關標籤/搜索