AcWing 36. 合并两个排序的链表 - Java
原题链接
简单
作者:
KYCygni
,
2021-02-25 01:56:23
,
所有人可见
,
阅读 245
Java 代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode merge(ListNode l1, ListNode l2)
{
ListNode ln = new ListNode(-1);
ListNode currNode = ln;
while (l1 != null && l2 != null)
{
if (l1.val < l2.val)
{
currNode.next = l1;
l1 = l1.next;
}
else
{
currNode.next = l2;
l2 = l2.next;
}
currNode = currNode.next;
}
if (l1 != null)
{
currNode.next = l1;
}
if (l2 != null)
{
currNode.next = l2;
}
return ln.next;
}
}