leetcode刷题32

今天刷的题比较简单,LeetCode第21题。这个题的意思是,给定两个链表,是有序的,要求合并,成一个有序的单链表

这个题就是双指针,具体地代码如下:

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode demo=new ListNode(0);
        ListNode current =demo;
        while (l1!=null&&l2!=null){
            if (l1.val<l2.val){
                current.next=l1;
                current=current.next;
                l1=l1.next;
            }else {
                current.next=l2;
                current=current.next;
                l2=l2.next;
            }
        }
        if (l1==null){
            current.next=l2;
        }else {
            current.next=l1;
        }
        return demo.next;
    }
}
原文地址:https://www.cnblogs.com/cquer-xjtuer-lys/p/11521934.html