Files
Hot-100-Algorithm/链表/合并两个有序链表/main.go
JACKYMYPERSON b2e4a26c09 增加新题目
2025-08-24 01:04:53 +08:00

72 lines
996 B
Go

package main
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
func main() {
}
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
upP := list1
dnP := list2
firstNode := (*ListNode)(nil)
value := 0
Ponit := (*ListNode)(nil)
if list1 == nil {
if list2 == nil {
return nil
}
return list2
}
if list2 == nil {
return list1
}
for upP != nil || dnP != nil {
tmpP := &ListNode{}
if upP == nil {
tmpP.Val = dnP.Val
Ponit.Next = tmpP
dnP = dnP.Next
Ponit = Ponit.Next
fmt.Println("停留1")
continue
}
if dnP == nil {
tmpP.Val = upP.Val
Ponit.Next = tmpP
upP = upP.Next
Ponit = Ponit.Next
fmt.Println("停留2")
continue
}
if upP.Val >= dnP.Val {
tmpP.Val = dnP.Val
dnP = dnP.Next
} else {
tmpP.Val = upP.Val
upP = upP.Next
}
if value == 0 {
Ponit = tmpP
firstNode = Ponit
value++
continue
}
Ponit.Next = tmpP
Ponit = Ponit.Next
value++
}
return firstNode
}