u
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package top100liked
|
package top100liked
|
||||||
|
|
||||||
|
// https://leetcode.cn/problems/search-a-2d-matrix-ii/?envType=study-plan-v2&envId=top-100-liked
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package top100liked
|
||||||
|
|
||||||
|
// https://leetcode.cn/problems/intersection-of-two-linked-lists/?envType=study-plan-v2&envId=top-100-liked
|
||||||
|
|
||||||
|
type ListNode struct {
|
||||||
|
Val int
|
||||||
|
Next *ListNode
|
||||||
|
}
|
||||||
|
|
||||||
|
func getIntersectionNode(headA, headB *ListNode) *ListNode {
|
||||||
|
vis := map[*ListNode]bool{}
|
||||||
|
for tmp := headA; tmp != nil; tmp = tmp.Next {
|
||||||
|
vis[tmp] = true
|
||||||
|
}
|
||||||
|
for tmp := headB; tmp != nil; tmp = tmp.Next {
|
||||||
|
if vis[tmp] {
|
||||||
|
return tmp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package top100liked
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://leetcode.cn/problems/reverse-linked-list/description/?envType=study-plan-v2&envId=top-100-liked
|
||||||
|
|
||||||
|
type ListNode struct {
|
||||||
|
Val int
|
||||||
|
Next *ListNode
|
||||||
|
}
|
||||||
|
|
||||||
|
func reverseList(head *ListNode) *ListNode {
|
||||||
|
if head == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var last *ListNode = nil
|
||||||
|
h := head
|
||||||
|
for h.Next != nil {
|
||||||
|
tmp := h.Next
|
||||||
|
h.Next = last
|
||||||
|
last = h
|
||||||
|
h = tmp
|
||||||
|
}
|
||||||
|
h.Next = last
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestS23(t *testing.T) {
|
||||||
|
input := ListNode{
|
||||||
|
Val: 1,
|
||||||
|
Next: &ListNode{
|
||||||
|
Val: 2,
|
||||||
|
Next: &ListNode{
|
||||||
|
Val: 3,
|
||||||
|
Next: &ListNode{
|
||||||
|
Val: 4,
|
||||||
|
Next: &ListNode{
|
||||||
|
Val: 5,
|
||||||
|
Next: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
output := reverseList(&input)
|
||||||
|
|
||||||
|
for output.Next != nil {
|
||||||
|
fmt.Println(output.Val)
|
||||||
|
output = output.Next
|
||||||
|
}
|
||||||
|
fmt.Println(output.Val)
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package top100liked
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://leetcode.cn/problems/palindrome-linked-list/description/?envType=study-plan-v2&envId=top-100-liked
|
||||||
|
|
||||||
|
type ListNode struct {
|
||||||
|
Val int
|
||||||
|
Next *ListNode
|
||||||
|
}
|
||||||
|
|
||||||
|
func isPalindrome(head *ListNode) bool {
|
||||||
|
vals := []int{}
|
||||||
|
for ; head != nil; head = head.Next {
|
||||||
|
vals = append(vals, head.Val)
|
||||||
|
}
|
||||||
|
n := len(vals)
|
||||||
|
for i, v := range vals[:n/2] {
|
||||||
|
if v != vals[n-1-i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestS24(t *testing.T) {
|
||||||
|
input := ListNode{
|
||||||
|
Val: 1,
|
||||||
|
Next: &ListNode{
|
||||||
|
Val: 2,
|
||||||
|
Next: &ListNode{
|
||||||
|
Val: 2,
|
||||||
|
Next: &ListNode{
|
||||||
|
Val: 1,
|
||||||
|
Next: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fmt.Println(isPalindrome(&input))
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package top100liked
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://leetcode.cn/problems/linked-list-cycle/description/?envType=study-plan-v2&envId=top-100-liked
|
||||||
|
|
||||||
|
type ListNode struct {
|
||||||
|
Val int
|
||||||
|
Next *ListNode
|
||||||
|
}
|
||||||
|
|
||||||
|
// func hasCycle(head *ListNode) bool {
|
||||||
|
// _map := make(map[*ListNode]bool)
|
||||||
|
// for head != nil {
|
||||||
|
// if _map[head] {
|
||||||
|
// return true
|
||||||
|
// }
|
||||||
|
// _map[head] = true
|
||||||
|
// head = head.Next
|
||||||
|
// }
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
|
||||||
|
func hasCycle(head *ListNode) bool {
|
||||||
|
if head == nil || head.Next == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
slow, fast := head, head.Next
|
||||||
|
for fast != slow {
|
||||||
|
if fast == nil || fast.Next == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
slow = slow.Next
|
||||||
|
fast = fast.Next.Next
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestS24(t *testing.T) {
|
||||||
|
input := &ListNode{
|
||||||
|
Val: 3,
|
||||||
|
Next: &ListNode{
|
||||||
|
Val: 2,
|
||||||
|
Next: &ListNode{
|
||||||
|
Val: 0,
|
||||||
|
Next: &ListNode{
|
||||||
|
Val: -4,
|
||||||
|
Next: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
input.Next.Next.Next.Next = input.Next
|
||||||
|
fmt.Println(hasCycle(input))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user