|
| 1 | +--- |
| 2 | +title: 143. Reorder List |
| 3 | +subtitle: "https://leetcode.com/problems/reorder-list/description/" |
| 4 | +date: 2024-03-03T16:53:00+08:00 |
| 5 | +lastmod: 2024-03-03T16:53:00+08:00 |
| 6 | +draft: false |
| 7 | +author: "Kimi.Tsai" |
| 8 | +authorLink: "https://kimi0230.github.io/" |
| 9 | +description: "0143.Reorder-List" |
| 10 | +license: "" |
| 11 | +images: [] |
| 12 | + |
| 13 | +tags: [LeetCode, Go, /Medium, 143. Reorder List, Amazon,Microsoft,Adobe,Facebook,Bloomberg,Linked List,Two Pointers,Stack, Recursion] |
| 14 | +categories: [LeetCode] |
| 15 | + |
| 16 | +featuredImage: "" |
| 17 | +featuredImagePreview: "" |
| 18 | + |
| 19 | +hiddenFromHomePage: false |
| 20 | +hiddenFromSearch: false |
| 21 | +twemoji: false |
| 22 | +lightgallery: true |
| 23 | +ruby: true |
| 24 | +fraction: true |
| 25 | +fontawesome: true |
| 26 | +linkToMarkdown: false |
| 27 | +rssFullText: false |
| 28 | + |
| 29 | +toc: |
| 30 | + enable: true |
| 31 | + auto: true |
| 32 | +code: |
| 33 | + copy: true |
| 34 | + maxShownLines: 200 |
| 35 | +math: |
| 36 | + enable: false |
| 37 | + # ... |
| 38 | +mapbox: |
| 39 | + # ... |
| 40 | +share: |
| 41 | + enable: true |
| 42 | + # ... |
| 43 | +comment: |
| 44 | + enable: true |
| 45 | + # ... |
| 46 | +library: |
| 47 | + css: |
| 48 | + # someCSS = "some.css" |
| 49 | + # located in "assets/" |
| 50 | + # Or |
| 51 | + # someCSS = "https://cdn.example.com/some.css" |
| 52 | + js: |
| 53 | + # someJS = "some.js" |
| 54 | + # located in "assets/" |
| 55 | + # Or |
| 56 | + # someJS = "https://cdn.example.com/some.js" |
| 57 | +seo: |
| 58 | + images: [] |
| 59 | + # ... |
| 60 | +--- |
| 61 | +# [143. Reorder List](https://leetcode.com/problems/reorder-list/description/) |
| 62 | + |
| 63 | +## 題目 |
| 64 | + |
| 65 | +## 題目大意 |
| 66 | + |
| 67 | + |
| 68 | +## 解題思路 |
| 69 | + |
| 70 | +## Big O |
| 71 | + |
| 72 | +* 時間複雜 : `` |
| 73 | +* 空間複雜 : `` |
| 74 | + |
| 75 | +## 來源 |
| 76 | +* https://leetcode.com/problems/reorder-list/description/ |
| 77 | +* https://leetcode.cn/problems/ |
| 78 | + |
| 79 | +## 解答 |
| 80 | +https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0143.Reorder-List/main.go |
| 81 | + |
| 82 | +```go |
| 83 | +package reorderlist |
| 84 | + |
| 85 | +/** |
| 86 | + * Definition for singly-linked list. |
| 87 | + * type ListNode struct { |
| 88 | + * Val int |
| 89 | + * Next *ListNode |
| 90 | + * } |
| 91 | + */ |
| 92 | + |
| 93 | +// 時間複雜 O(), 空間複雜 O() |
| 94 | +// 先用快慢指針找出Linked list的中間點 |
| 95 | +// 然後把Linked list分成兩半 |
| 96 | +// 再把後半的Linked list反轉 |
| 97 | +// 再把兩半的Linked list merge 起來 |
| 98 | +func reorderList(head *ListNode) { |
| 99 | + mid := middleNode(head) |
| 100 | + |
| 101 | + // 2.反轉中間節點的下一個節點 |
| 102 | + right := resverseNode(mid.Next) |
| 103 | + mid.Next = nil |
| 104 | + left := head |
| 105 | + mergeNode(left, right) |
| 106 | +} |
| 107 | + |
| 108 | +// [876. Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) |
| 109 | +func middleNode(head *ListNode) *ListNode { |
| 110 | + slow, fast := head, head |
| 111 | + for fast != nil && fast.Next != nil { |
| 112 | + slow = slow.Next |
| 113 | + fast = fast.Next.Next |
| 114 | + } |
| 115 | + return slow |
| 116 | +} |
| 117 | + |
| 118 | +// [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) |
| 119 | +func resverseNode(head *ListNode) *ListNode { |
| 120 | + var pre *ListNode |
| 121 | + for head != nil { |
| 122 | + tmp := head.Next |
| 123 | + head.Next = pre |
| 124 | + pre = head |
| 125 | + head = tmp |
| 126 | + } |
| 127 | + return pre |
| 128 | +} |
| 129 | + |
| 130 | +func mergeNode(l1, l2 *ListNode) { |
| 131 | + lcur, rcur := l1, l2 |
| 132 | + for lcur != nil && rcur != nil { |
| 133 | + lcur.Next, rcur.Next, lcur, rcur = rcur, lcur.Next, lcur.Next, rcur.Next |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +``` |
| 138 | + |
| 139 | +## Benchmark |
| 140 | + |
| 141 | +```sh |
| 142 | + |
| 143 | +``` |
0 commit comments