LeetCode-21. 合并两个有序链表

乐云一
  • 刷题日记
  • LeetCode
About 160 wordsLess than 1 minute

示例

QQ截图20210923102315.png

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]

思路

根据图所示理解起来比较简单:遍历两条链表,然后逐一判断大小,将节点加至新链表中。

代码

 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode result=new ListNode(-1);
        ListNode temp=result;
        while(l1!=null&&l2!=null){
            if(l1.val>l2.val){
                temp.next=l2;
                l2=l2.next;
            }else {
                temp.next=l1;
                l1=l1.next;
            }
            temp=temp.next;
        }
        temp.next= l1==null? l2: l1;
        return result.next;
    }
Last update:
Contributors: leyunone
Comments
  • Latest
  • Oldest
  • Hottest
Powered by Waline v2.14.7