using System;
using System.Collections.Generic;
/*
public class ListNode
{
public int val;
public ListNode next;
public ListNode (int x)
{
val = x;
}
}
*/
class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
public ListNode Merge (ListNode pHead1, ListNode pHead2) {
ListNode list1 = pHead1;
ListNode list2 = pHead2;
// write code here
ListNode newhead = new ListNode(0);
ListNode cur = newhead;
while (true) {
if (list1 == null) {
cur.next = list2;
break;
}
if (list2 == null) {
cur.next = list1;
break;
}
if (list1.val > list2.val) {
cur.next = list2;
cur = cur.next;
list2 = list2.next;
} else {
cur.next = list1;
cur = cur.next;
list1 = list1.next;
}
}
return newhead.next;
}
}