Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
创建一个新结点,同时遍历两个链表,取较小的一个添加到新结点后面。
最后再把剩下的节点加到链表后面
public class Solution {
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
ListNode curNode = null;
ListNode headNode = new ListNode(-1);
curNode = headNode;
while (l1 != null && l2 != null)
{
if (l1.val < l2.val)
{
curNode.next = l1;
l1 = l1.next;
}
else
{
curNode.next = l2;
l2 = l2.next;
}
curNode = curNode.next;
}
if (l2 != null)
{
curNode.next = l2;
}
else if (l1 != null)
{
curNode.next = l1;
}
return headNode.next;
}
}