mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 17:44:10 +08:00
规范格式
This commit is contained in:
89
leetcode/0143.Reorder-List/143. Reorder List.go
Normal file
89
leetcode/0143.Reorder-List/143. Reorder List.go
Normal file
@ -0,0 +1,89 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
* Val int
|
||||
* Next *ListNode
|
||||
* }
|
||||
*/
|
||||
|
||||
// 解法一 单链表
|
||||
func reorderList(head *ListNode) *ListNode {
|
||||
if head == nil || head.Next == nil {
|
||||
return head
|
||||
}
|
||||
|
||||
// 寻找中间结点
|
||||
p1 := head
|
||||
p2 := head
|
||||
for p2.Next != nil && p2.Next.Next != nil {
|
||||
p1 = p1.Next
|
||||
p2 = p2.Next.Next
|
||||
}
|
||||
|
||||
// 反转链表后半部分 1->2->3->4->5->6 to 1->2->3->6->5->4
|
||||
preMiddle := p1
|
||||
preCurrent := p1.Next
|
||||
for preCurrent.Next != nil {
|
||||
current := preCurrent.Next
|
||||
preCurrent.Next = current.Next
|
||||
current.Next = preMiddle.Next
|
||||
preMiddle.Next = current
|
||||
}
|
||||
|
||||
// 重新拼接链表 1->2->3->6->5->4 to 1->6->2->5->3->4
|
||||
p1 = head
|
||||
p2 = preMiddle.Next
|
||||
for p1 != preMiddle {
|
||||
preMiddle.Next = p2.Next
|
||||
p2.Next = p1.Next
|
||||
p1.Next = p2
|
||||
p1 = p2.Next
|
||||
p2 = preMiddle.Next
|
||||
}
|
||||
return head
|
||||
}
|
||||
|
||||
// 解法二 数组
|
||||
func reorderList1(head *ListNode) *ListNode {
|
||||
array := listToArray(head)
|
||||
length := len(array)
|
||||
if length == 0 {
|
||||
return head
|
||||
}
|
||||
cur := head
|
||||
last := head
|
||||
for i := 0; i < len(array)/2; i++ {
|
||||
tmp := &ListNode{Val: array[length-1-i], Next: cur.Next}
|
||||
cur.Next = tmp
|
||||
cur = tmp.Next
|
||||
last = tmp
|
||||
}
|
||||
if length%2 == 0 {
|
||||
last.Next = nil
|
||||
} else {
|
||||
cur.Next = nil
|
||||
}
|
||||
return head
|
||||
}
|
||||
|
||||
func listToArray(head *ListNode) []int {
|
||||
array := []int{}
|
||||
if head == nil {
|
||||
return array
|
||||
}
|
||||
cur := head
|
||||
for cur != nil {
|
||||
array = append(array, cur.Val)
|
||||
cur = cur.Next
|
||||
}
|
||||
return array
|
||||
}
|
58
leetcode/0143.Reorder-List/143. Reorder List_test.go
Normal file
58
leetcode/0143.Reorder-List/143. Reorder List_test.go
Normal file
@ -0,0 +1,58 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question143 struct {
|
||||
para143
|
||||
ans143
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para143 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans143 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem143(t *testing.T) {
|
||||
|
||||
qs := []question143{
|
||||
|
||||
question143{
|
||||
para143{[]int{1, 2, 3, 4, 5}},
|
||||
ans143{[]int{1, 5, 2, 4, 3}},
|
||||
},
|
||||
question143{
|
||||
para143{[]int{1, 2, 3, 4}},
|
||||
ans143{[]int{1, 4, 2, 3}},
|
||||
},
|
||||
|
||||
question143{
|
||||
para143{[]int{1}},
|
||||
ans143{[]int{1}},
|
||||
},
|
||||
|
||||
question143{
|
||||
para143{[]int{}},
|
||||
ans143{[]int{}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 143------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans143, q.para143
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(reorderList(structures.Ints2List(p.one))))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
34
leetcode/0143.Reorder-List/README.md
Normal file
34
leetcode/0143.Reorder-List/README.md
Normal file
@ -0,0 +1,34 @@
|
||||
# [143. Reorder List](https://leetcode.com/problems/reorder-list/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
|
||||
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
|
||||
|
||||
You may not modify the values in the list's nodes, only nodes itself may be changed.
|
||||
|
||||
Example 1:
|
||||
|
||||
```c
|
||||
Given 1->2->3->4, reorder it to 1->4->2->3.
|
||||
```
|
||||
|
||||
Example 2:
|
||||
|
||||
```c
|
||||
Given 1->2->3->4->5, reorder it to 1->5->2->4->3.
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
按照指定规则重新排序链表:第一个元素和最后一个元素排列在一起,接着第二个元素和倒数第二个元素排在一起,接着第三个元素和倒数第三个元素排在一起。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
|
||||
最近简单的方法是先把链表存储到数组里,然后找到链表中间的结点,按照规则拼接即可。这样时间复杂度是 O(n),空间复杂度是 O(n)。
|
||||
|
||||
更好的做法是结合之前几道题的操作:链表逆序,找中间结点。
|
||||
|
||||
先找到链表的中间结点,然后利用逆序区间的操作,如 [第 92 题](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0092.Reverse-Linked-List-II) 里的 reverseBetween() 操作,只不过这里的反转区间是从中点一直到末尾。最后利用 2 个指针,一个指向头结点,一个指向中间结点,开始拼接最终的结果。这种做法的时间复杂度是 O(n),空间复杂度是 O(1)。
|
Reference in New Issue
Block a user