Skip to content

Commit dcd0daf

Browse files
committed
feat: 0002.Add-Two-Numbers & 0143.Reorder-List
1 parent 1ea7649 commit dcd0daf

File tree

5 files changed

+342
-0
lines changed

5 files changed

+342
-0
lines changed

Leetcode/0143.Reorder-List/README.md

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
```

Leetcode/0143.Reorder-List/main.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package reorderlist
2+
3+
/**
4+
* Definition for singly-linked list.
5+
* type ListNode struct {
6+
* Val int
7+
* Next *ListNode
8+
* }
9+
*/
10+
11+
// 時間複雜 O(), 空間複雜 O()
12+
// 先用快慢指針找出Linked list的中間點
13+
// 然後把Linked list分成兩半
14+
// 再把後半的Linked list反轉
15+
// 再把兩半的Linked list merge 起來
16+
func reorderList(head *ListNode) {
17+
mid := middleNode(head)
18+
19+
// 2.反轉中間節點的下一個節點
20+
right := resverseNode(mid.Next)
21+
mid.Next = nil
22+
left := head
23+
mergeNode(left, right)
24+
}
25+
26+
// [876. Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)
27+
func middleNode(head *ListNode) *ListNode {
28+
slow, fast := head, head
29+
for fast != nil && fast.Next != nil {
30+
slow = slow.Next
31+
fast = fast.Next.Next
32+
}
33+
return slow
34+
}
35+
36+
// [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)
37+
func resverseNode(head *ListNode) *ListNode {
38+
var pre *ListNode
39+
for head != nil {
40+
tmp := head.Next
41+
head.Next = pre
42+
pre = head
43+
head = tmp
44+
}
45+
return pre
46+
}
47+
48+
func mergeNode(l1, l2 *ListNode) {
49+
lcur, rcur := l1, l2
50+
for lcur != nil && rcur != nil {
51+
lcur.Next, rcur.Next, lcur, rcur = rcur, lcur.Next, lcur.Next, rcur.Next
52+
}
53+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
arg1 string
7+
want int
8+
}{
9+
{
10+
"bbbab",
11+
4,
12+
},
13+
}
14+
15+
func TestLongestPalindromeSubseq(t *testing.T) {
16+
for _, tt := range tests {
17+
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) {
18+
if got := LongestPalindromeSubseq(tt.arg1); got != tt.want {
19+
t.Errorf("got = %v, want = %v", got, tt.want)
20+
}
21+
}
22+
}
23+
24+
func BenchmarkLongestPalindromeSubseq(b *testing.B) {
25+
b.ResetTimer()
26+
for i := 0; i < b.N; i++ {
27+
LongestPalindromeSubseq(tests[0].arg1)
28+
}
29+
}
30+
31+
/*
32+
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.Russian-Doll-Envelopes -bench=.
33+
34+
*/
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// 時間複雜度:
2+
// 空間複雜度:
3+
/*
4+
* @lc app=leetcode.cn id=143 lang=golang
5+
*
6+
* [143] 重排链表
7+
*/
8+
9+
// @lc code=start
10+
/**
11+
* Definition for singly-linked list.
12+
* type ListNode struct {
13+
* Val int
14+
* Next *ListNode
15+
* }
16+
*/
17+
18+
// 先用快慢指針找出Linked list的中間點
19+
// 然後把Linked list分成兩半
20+
// 再把後半的Linked list反轉
21+
// 再把兩半的Linked list merge 起來
22+
func reorderList(head *ListNode) {
23+
mid := middleNode(head)
24+
25+
// 2.反轉中間節點的下一個節點
26+
right := resverseNode(mid.Next)
27+
mid.Next = nil
28+
left := head
29+
mergeNode(left, right)
30+
}
31+
32+
// [876. Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)
33+
func middleNode(head *ListNode) *ListNode {
34+
slow, fast := head, head
35+
for fast != nil && fast.Next != nil {
36+
slow = slow.Next
37+
fast = fast.Next.Next
38+
}
39+
return slow
40+
}
41+
42+
// [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)
43+
func resverseNode(head *ListNode) *ListNode {
44+
var pre *ListNode
45+
for head != nil {
46+
tmp := head.Next
47+
head.Next = pre
48+
pre = head
49+
head = tmp
50+
}
51+
return pre
52+
}
53+
54+
func mergeNode(l1, l2 *ListNode) {
55+
lcur, rcur := l1, l2
56+
for lcur != nil && rcur != nil {
57+
lcur.Next, rcur.Next, lcur, rcur = rcur, lcur.Next, lcur.Next, rcur.Next
58+
}
59+
}
60+
61+
// @lc code=end
62+

Leetcode_labuladong/2.两数相加.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// 時間複雜度:
2+
// 空間複雜度:
3+
/*
4+
* @lc app=leetcode.cn id=2 lang=golang
5+
*
6+
* [2] 两数相加
7+
*/
8+
9+
// @lc code=start
10+
/**
11+
* Definition for singly-linked list.
12+
* type ListNode struct {
13+
* Val int
14+
* Next *ListNode
15+
* }
16+
*/
17+
// 遍歷 l1跟 l2. 講兩個list的val相加, 並且記錄進位的值給next使用
18+
// 最後如果 carry 還有的話, 需要產生一個新的節點
19+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
20+
var result, tail *ListNode
21+
carry := 0
22+
for l1 != nil || l2 != nil {
23+
n1, n2 := 0, 0
24+
if l1 != nil {
25+
n1 = l1.Val
26+
l1 = l1.Next
27+
}
28+
if l2 != nil {
29+
n2 = l2.Val
30+
l2 = l2.Next
31+
}
32+
33+
sum := n1 + n2 + carry
34+
sum, carry = sum%10, sum/10
35+
if result == nil {
36+
result = &ListNode{Val: sum}
37+
tail = result
38+
} else {
39+
tail.Next = &ListNode{Val: sum}
40+
tail = tail.Next
41+
}
42+
}
43+
if carry > 0 {
44+
tail.Next = &ListNode{Val: carry}
45+
}
46+
return result
47+
}
48+
49+
// @lc code=end
50+

0 commit comments

Comments
 (0)