添加新题目
This commit is contained in:
25
链表/反转链表/main.go
Normal file
25
链表/反转链表/main.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
func reverseList(head *ListNode) *ListNode {
|
||||
pre := (*ListNode)(nil)
|
||||
res := (*ListNode)(nil)
|
||||
for head != nil {
|
||||
next := head.Next
|
||||
head.Next = pre
|
||||
pre = head
|
||||
if next == nil {
|
||||
res = head
|
||||
break
|
||||
}
|
||||
head = next
|
||||
}
|
||||
return res
|
||||
}
|
||||
20
链表/合并两个有序链表/main.go
Normal file
20
链表/合并两个有序链表/main.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
|
||||
upP := list1
|
||||
dnP := list2
|
||||
for upP.Next != nil && dnP.Next != nil {
|
||||
if upP.Next == nil {
|
||||
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
40
链表/回文链表/main.go
Normal file
40
链表/回文链表/main.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
func isPalindrome(head *ListNode) bool {
|
||||
numlist := make([]int, 0)
|
||||
for head != nil {
|
||||
numlist = append(numlist, head.Val)
|
||||
head = head.Next
|
||||
}
|
||||
if len(numlist) == 1 {
|
||||
return true
|
||||
}
|
||||
left := len(numlist)/2 - 1
|
||||
right := left + 1
|
||||
if len(numlist)%2 != 0 {
|
||||
left = len(numlist) / 2
|
||||
right = left
|
||||
} else {
|
||||
left = len(numlist)/2 - 1
|
||||
right = left + 1
|
||||
}
|
||||
|
||||
for left >= 0 {
|
||||
if numlist[left] == numlist[right] {
|
||||
left--
|
||||
right++
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
23
链表/环形链表/main.go
Normal file
23
链表/环形链表/main.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
|
||||
func hasCycle(head *ListNode) bool {
|
||||
hash := make(map[*ListNode]struct{})
|
||||
for head != nil {
|
||||
if _, ok := hash[head.Next]; ok {
|
||||
return true
|
||||
}
|
||||
hash[head] = struct{}{}
|
||||
head = head.Next
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
22
链表/环形链表2/main.go
Normal file
22
链表/环形链表2/main.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
func detectCycle(head *ListNode) *ListNode {
|
||||
hash := make(map[*ListNode]struct{})
|
||||
for head != nil {
|
||||
if _, ok := hash[head.Next]; ok {
|
||||
return head.Next
|
||||
}
|
||||
hash[head] = struct{}{}
|
||||
head = head.Next
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
28
链表/相交链表/main.go
Normal file
28
链表/相交链表/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
func getIntersectionNode(headA, headB *ListNode) *ListNode {
|
||||
Anode, Bnode := headA, headB
|
||||
hash := make(map[*ListNode]struct{})
|
||||
|
||||
for Anode != nil {
|
||||
hash[Anode] = struct{}{}
|
||||
Anode = Anode.Next
|
||||
}
|
||||
for Bnode != nil {
|
||||
if _, ok := hash[Bnode]; ok {
|
||||
return Bnode
|
||||
}
|
||||
Bnode = Bnode.Next
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user