Files

72 lines
996 B
Go
Raw Permalink Normal View History

2025-08-23 23:09:54 +08:00
package main
2025-08-24 01:04:53 +08:00
import "fmt"
2025-08-23 23:09:54 +08:00
type ListNode struct {
Val int
Next *ListNode
}
func main() {
}
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
upP := list1
dnP := list2
2025-08-24 01:04:53 +08:00
firstNode := (*ListNode)(nil)
value := 0
Ponit := (*ListNode)(nil)
if list1 == nil {
if list2 == nil {
return nil
}
return list2
2025-08-23 23:09:54 +08:00
2025-08-24 01:04:53 +08:00
}
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
2025-08-23 23:09:54 +08:00
}
2025-08-24 01:04:53 +08:00
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++
2025-08-23 23:09:54 +08:00
}
2025-08-24 01:04:53 +08:00
return firstNode
2025-08-23 23:09:54 +08:00
}