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 }