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

[Swift]LeetCode143. 重排链表 | Reorder List

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

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

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

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

Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→Ln-1→L2→Ln-2→…

You may not modify the values in the list's nodes, only nodes itself may be changed.

Example 1:

Given 1->2->3->4, reorder it to 1->4->2->3.

Example 2:

Given 1->2->3->4->5, reorder it to 1->5->2->4->3.

给定一个单链表 LL0→L1→…→Ln-1→Ln ,
将其重新排列后变为: L0→LnL1→Ln-1→L2→Ln-2→…

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

示例 1:

给定链表 1->2->3->4, 重新排列为 1->4->2->3.

示例 2:

给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.

72ms
 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 reorderList(_ head: ListNode?) {
14         var arr = [ListNode]()
15         var cur = head
16         while cur != nil {
17             arr.append(cur!)
18             cur = cur!.next
19         }
20         var i = 0
21         var j = arr.count - 1
22         
23         cur = nil
24         while i <= j {
25             if i == j {
26                 cur?.next = arr[j]
27                 cur = arr[j]
28                 break
29             }
30             
31             cur?.next = arr[i]
32             arr[i].next = arr[j]
33             cur = arr[j]
34             
35             i += 1
36             j -= 1
37         }
38         cur?.next = nil
39     }
40 }

80ms

 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 reorderList(_ head: ListNode?) -> ListNode? {
14         guard let head = head else { return nil }
15         var slow: ListNode? = head
16         var fast: ListNode? = head
17         while fast?.next?.next != nil {
18             fast = fast?.next?.next
19             slow = slow?.next
20         }
21         let next = reverseList(slow?.next)
22         slow?.next = nil
23         slow = head
24         fast = next
25         var curr: ListNode? = nil
26         while slow != nil {
27             curr?.next = slow
28             curr = fast
29             let tmp = slow?.next
30             slow?.next = fast
31             slow = tmp
32             fast = fast?.next
33         }
34         return head
35     }
36     
37     func reverseList(_ head: ListNode?) -> ListNode? {
38         guard let head = head else { return nil }
39         var curr: ListNode? = head
40         var prev: ListNode? = nil 
41         while curr != nil {
42             let tmp = curr?.next
43             curr?.next = prev
44             prev = curr
45             curr = tmp
46         }
47         return prev
48     }
49 }

84ms

 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 reorderList(_ head: ListNode?) {
14         if head?.next?.next == nil { return }
15         
16         var slow: ListNode? = head
17         var quick: ListNode? = head
18         
19         while (true) {
20             if quick?.next?.next == nil { break }
21             slow = slow?.next
22             quick = quick?.next?.next
23         }
24         
25         // 1 // 1, 2
26         // if slow?.val == quick?.val { return }
27 
28         // 1,2,3
29         var second: ListNode = slow!.next!
30         slow!.next = nil
31         var first: ListNode = head!
32         
33         
34         revNode(&second)
35         
36         // debug(first)
37         // debug(second)
38         let resHead = mergNode(first, second)
39         // debug(resHead)
40         
41         head?.next = resHead.next
42     }
43     
44     func revNode(_ head: inout ListNode) {
45         var prev: ListNode?
46         var cur: ListNode? = head
47         
48         while (cur != nil) {
49             let temp: ListNode? = cur?.next
50             cur?.next = prev
51             prev = cur
52             cur = temp
53         }
54         
55         head = prev!
56     }
57     
58     func mergNode(_ n1: ListNode, _ n2: ListNode) -> ListNode {
59         var cur1: ListNode? = n1
60         var cur2: ListNode? = n2
61         
62         let head: ListNode = ListNode(0)
63         var cur: ListNode? = head
64         
65         var m1 = true
66         while (true) {
67             if m1 {
68                 cur?.next = cur1
69                 cur = cur?.next
70                 cur1 = cur1?.next
71             } else {
72                 cur?.next = cur2
73                 cur = cur?.next
74                 cur2 = cur2?.next
75             }
76             m1 = !m1
77             if cur1 == nil && cur2 == nil { break }
78         }
79         
80         return head.next!
81     }
82     
83     func debug(_ node: ListNode?) {
84         var temp: ListNode? = node
85         var res = ""
86         while temp != nil {
87             res += String(temp!.val) + " "
88             temp = temp!.next
89         }
90         print(res)
91     }
92 }

92ms

 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 reorderList(_ head: ListNode?) {
14         if head == nil || head?.next == nil {
15             return
16         }
17         
18         var p1: ListNode? = head
19         var p2: ListNode? = head
20         while p2?.next != nil && p2?.next?.next != nil {
21             p1 = p1?.next
22             p2 = p2?.next?.next
23         }
24         
25         //reverse the 2nd half
26         let preMiddle: ListNode? = p1
27         let preCur: ListNode? = p1?.next
28         while preCur?.next != nil {
29             let cur = preCur?.next
30             preCur?.next = cur?.next
31             cur?.next = preMiddle?.next
32             preMiddle?.next = cur
33         }
34         
35         // reconstruct the link 
36         p1 = head
37         p2 = preMiddle?.next
38         while p1 !== preMiddle {
39             preMiddle?.next = p2?.next
40             p2?.next = p1?.next
41             p1?.next = p2
42             p1 = p2?.next
43             p2 = preMiddle?.next
44         }
45     }
46 }

104ms

 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 reorderList(_ head: ListNode?) {
14         guard let head = head else {
15             return
16         }
17         
18         var next: ListNode? = head
19         var reverseNodes: [ListNode] = []
20         while next != nil {
21             reverseNodes.append(next!)
22             next = next?.next
23         }
24 
25         next = head
26         var index = reverseNodes.count - 1
27         while index >= reverseNodes.count / 2 {
28             let temp = next?.next
29             next?.next = reverseNodes[index]
30             reverseNodes[index].next = temp
31             next = temp
32 
33             index -= 1
34         }
35 
36         reverseNodes[index + 1].next = nil
37     }
38 }

108ms

 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 reorderList(_ head: ListNode?) {
14         guard let head = head else {
15             return
16         }
17   
18         var prev: ListNode? = head
19         var post: ListNode? = head
20   
21         _split(&prev, &post)
22         prev = head
23   
24         post = _reverse(&post)
25   
26         _merge(&prev, &post)
27     }
28     
29      private func _split(_ prev: inout ListNode?, _ post: inout ListNode?) {
30         while post != nil && post!.next != nil {
31             prev = prev!.next
32             post = post!.next!.next
33         }
34   
35         post = prev!.next
36         prev!.next = nil
37     }
38 
39     private func _reverse(_ head:inout ListNode?) -> ListNode? {
40         var prev = head
41         var temp: ListNode?
42   
43         while prev != nil {
44             let post = prev!.next
45     
46             prev!.next = temp
47     
48             temp = prev
49             prev = post
50         }
51   
52         return temp
53     }
54 
55     private func _merge(_ prev:inout ListNode?, _ post:inout ListNode?) {
56         while prev != nil && post != nil{
57             let preNext = prev!.next
58             let posNext = post!.next
59     
60             prev!.next = post
61             post!.next = preNext
62     
63             prev = preNext
64             post = posNext
65         }
66     }
67 }

 

posted @ 2018-11-16 10:58  为敢技术  阅读(458)  评论(0编辑  收藏  举报