-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 0002.Add-Two-Numbers & 0143.Reorder-List
- Loading branch information
Showing
5 changed files
with
342 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
--- | ||
title: 143. Reorder List | ||
subtitle: "https://leetcode.com/problems/reorder-list/description/" | ||
date: 2024-03-03T16:53:00+08:00 | ||
lastmod: 2024-03-03T16:53:00+08:00 | ||
draft: false | ||
author: "Kimi.Tsai" | ||
authorLink: "https://kimi0230.github.io/" | ||
description: "0143.Reorder-List" | ||
license: "" | ||
images: [] | ||
|
||
tags: [LeetCode, Go, /Medium, 143. Reorder List, Amazon,Microsoft,Adobe,Facebook,Bloomberg,Linked List,Two Pointers,Stack, Recursion] | ||
categories: [LeetCode] | ||
|
||
featuredImage: "" | ||
featuredImagePreview: "" | ||
|
||
hiddenFromHomePage: false | ||
hiddenFromSearch: false | ||
twemoji: false | ||
lightgallery: true | ||
ruby: true | ||
fraction: true | ||
fontawesome: true | ||
linkToMarkdown: false | ||
rssFullText: false | ||
|
||
toc: | ||
enable: true | ||
auto: true | ||
code: | ||
copy: true | ||
maxShownLines: 200 | ||
math: | ||
enable: false | ||
# ... | ||
mapbox: | ||
# ... | ||
share: | ||
enable: true | ||
# ... | ||
comment: | ||
enable: true | ||
# ... | ||
library: | ||
css: | ||
# someCSS = "some.css" | ||
# located in "assets/" | ||
# Or | ||
# someCSS = "https://cdn.example.com/some.css" | ||
js: | ||
# someJS = "some.js" | ||
# located in "assets/" | ||
# Or | ||
# someJS = "https://cdn.example.com/some.js" | ||
seo: | ||
images: [] | ||
# ... | ||
--- | ||
# [143. Reorder List](https://leetcode.com/problems/reorder-list/description/) | ||
|
||
## 題目 | ||
|
||
## 題目大意 | ||
|
||
|
||
## 解題思路 | ||
|
||
## Big O | ||
|
||
* 時間複雜 : `` | ||
* 空間複雜 : `` | ||
|
||
## 來源 | ||
* https://leetcode.com/problems/reorder-list/description/ | ||
* https://leetcode.cn/problems/ | ||
|
||
## 解答 | ||
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0143.Reorder-List/main.go | ||
|
||
```go | ||
package reorderlist | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* type ListNode struct { | ||
* Val int | ||
* Next *ListNode | ||
* } | ||
*/ | ||
|
||
// 時間複雜 O(), 空間複雜 O() | ||
// 先用快慢指針找出Linked list的中間點 | ||
// 然後把Linked list分成兩半 | ||
// 再把後半的Linked list反轉 | ||
// 再把兩半的Linked list merge 起來 | ||
func reorderList(head *ListNode) { | ||
mid := middleNode(head) | ||
|
||
// 2.反轉中間節點的下一個節點 | ||
right := resverseNode(mid.Next) | ||
mid.Next = nil | ||
left := head | ||
mergeNode(left, right) | ||
} | ||
|
||
// [876. Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | ||
func middleNode(head *ListNode) *ListNode { | ||
slow, fast := head, head | ||
for fast != nil && fast.Next != nil { | ||
slow = slow.Next | ||
fast = fast.Next.Next | ||
} | ||
return slow | ||
} | ||
|
||
// [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | ||
func resverseNode(head *ListNode) *ListNode { | ||
var pre *ListNode | ||
for head != nil { | ||
tmp := head.Next | ||
head.Next = pre | ||
pre = head | ||
head = tmp | ||
} | ||
return pre | ||
} | ||
|
||
func mergeNode(l1, l2 *ListNode) { | ||
lcur, rcur := l1, l2 | ||
for lcur != nil && rcur != nil { | ||
lcur.Next, rcur.Next, lcur, rcur = rcur, lcur.Next, lcur.Next, rcur.Next | ||
} | ||
} | ||
|
||
``` | ||
|
||
## Benchmark | ||
|
||
```sh | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package reorderlist | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* type ListNode struct { | ||
* Val int | ||
* Next *ListNode | ||
* } | ||
*/ | ||
|
||
// 時間複雜 O(), 空間複雜 O() | ||
// 先用快慢指針找出Linked list的中間點 | ||
// 然後把Linked list分成兩半 | ||
// 再把後半的Linked list反轉 | ||
// 再把兩半的Linked list merge 起來 | ||
func reorderList(head *ListNode) { | ||
mid := middleNode(head) | ||
|
||
// 2.反轉中間節點的下一個節點 | ||
right := resverseNode(mid.Next) | ||
mid.Next = nil | ||
left := head | ||
mergeNode(left, right) | ||
} | ||
|
||
// [876. Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | ||
func middleNode(head *ListNode) *ListNode { | ||
slow, fast := head, head | ||
for fast != nil && fast.Next != nil { | ||
slow = slow.Next | ||
fast = fast.Next.Next | ||
} | ||
return slow | ||
} | ||
|
||
// [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | ||
func resverseNode(head *ListNode) *ListNode { | ||
var pre *ListNode | ||
for head != nil { | ||
tmp := head.Next | ||
head.Next = pre | ||
pre = head | ||
head = tmp | ||
} | ||
return pre | ||
} | ||
|
||
func mergeNode(l1, l2 *ListNode) { | ||
lcur, rcur := l1, l2 | ||
for lcur != nil && rcur != nil { | ||
lcur.Next, rcur.Next, lcur, rcur = rcur, lcur.Next, lcur.Next, rcur.Next | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package | ||
|
||
import "testing" | ||
|
||
var tests = []struct { | ||
arg1 string | ||
want int | ||
}{ | ||
{ | ||
"bbbab", | ||
4, | ||
}, | ||
} | ||
|
||
func TestLongestPalindromeSubseq(t *testing.T) { | ||
for _, tt := range tests { | ||
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) { | ||
if got := LongestPalindromeSubseq(tt.arg1); got != tt.want { | ||
t.Errorf("got = %v, want = %v", got, tt.want) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkLongestPalindromeSubseq(b *testing.B) { | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
LongestPalindromeSubseq(tests[0].arg1) | ||
} | ||
} | ||
|
||
/* | ||
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.Russian-Doll-Envelopes -bench=. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// 時間複雜度: | ||
// 空間複雜度: | ||
/* | ||
* @lc app=leetcode.cn id=143 lang=golang | ||
* | ||
* [143] 重排链表 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* Definition for singly-linked list. | ||
* type ListNode struct { | ||
* Val int | ||
* Next *ListNode | ||
* } | ||
*/ | ||
|
||
// 先用快慢指針找出Linked list的中間點 | ||
// 然後把Linked list分成兩半 | ||
// 再把後半的Linked list反轉 | ||
// 再把兩半的Linked list merge 起來 | ||
func reorderList(head *ListNode) { | ||
mid := middleNode(head) | ||
|
||
// 2.反轉中間節點的下一個節點 | ||
right := resverseNode(mid.Next) | ||
mid.Next = nil | ||
left := head | ||
mergeNode(left, right) | ||
} | ||
|
||
// [876. Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | ||
func middleNode(head *ListNode) *ListNode { | ||
slow, fast := head, head | ||
for fast != nil && fast.Next != nil { | ||
slow = slow.Next | ||
fast = fast.Next.Next | ||
} | ||
return slow | ||
} | ||
|
||
// [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | ||
func resverseNode(head *ListNode) *ListNode { | ||
var pre *ListNode | ||
for head != nil { | ||
tmp := head.Next | ||
head.Next = pre | ||
pre = head | ||
head = tmp | ||
} | ||
return pre | ||
} | ||
|
||
func mergeNode(l1, l2 *ListNode) { | ||
lcur, rcur := l1, l2 | ||
for lcur != nil && rcur != nil { | ||
lcur.Next, rcur.Next, lcur, rcur = rcur, lcur.Next, lcur.Next, rcur.Next | ||
} | ||
} | ||
|
||
// @lc code=end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 時間複雜度: | ||
// 空間複雜度: | ||
/* | ||
* @lc app=leetcode.cn id=2 lang=golang | ||
* | ||
* [2] 两数相加 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* Definition for singly-linked list. | ||
* type ListNode struct { | ||
* Val int | ||
* Next *ListNode | ||
* } | ||
*/ | ||
// 遍歷 l1跟 l2. 講兩個list的val相加, 並且記錄進位的值給next使用 | ||
// 最後如果 carry 還有的話, 需要產生一個新的節點 | ||
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { | ||
var result, tail *ListNode | ||
carry := 0 | ||
for l1 != nil || l2 != nil { | ||
n1, n2 := 0, 0 | ||
if l1 != nil { | ||
n1 = l1.Val | ||
l1 = l1.Next | ||
} | ||
if l2 != nil { | ||
n2 = l2.Val | ||
l2 = l2.Next | ||
} | ||
|
||
sum := n1 + n2 + carry | ||
sum, carry = sum%10, sum/10 | ||
if result == nil { | ||
result = &ListNode{Val: sum} | ||
tail = result | ||
} else { | ||
tail.Next = &ListNode{Val: sum} | ||
tail = tail.Next | ||
} | ||
} | ||
if carry > 0 { | ||
tail.Next = &ListNode{Val: carry} | ||
} | ||
return result | ||
} | ||
|
||
// @lc code=end | ||
|