package main
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
 * 
 * @param pHead1 ListNode类 
 * @param pHead2 ListNode类 
 * @return ListNode类
*/
func Merge( pHead1 *ListNode ,  pHead2 *ListNode ) *ListNode {
    if pHead1 == nil {
        return pHead2
    }
    if pHead2 == nil {
        return pHead1
    }
    
    p1 := pHead1
    p2 := pHead2

    var cur *ListNode 
    if p1.Val <= p2.Val {
        cur = p1
        p1 = p1.Next
    } else {
        cur = p2
        p2 = p2.Next
    }
    ret := cur

    for p1 != nil && p2 != nil {
        if p1.Val <= p2.Val {
            cur.Next = p1
            p1 = p1.Next
        } else {
            cur.Next = p2
            p2 = p2.Next
        }
        cur = cur.Next
    }

    if p1 != nil {
        cur.Next = p1
    }
    if p2 != nil {
        cur.Next = p2
    }
    return ret
}