Brush title leetcode 32

Today's question is relatively simple brush, LeetCode question 21. This problem means that, given two lists, ordered, requiring merge into a single ordered list

The problem is that the double pointer, particular code is as follows:

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;
    }
}

 

Guess you like

Origin www.cnblogs.com/cquer-xjtuer-lys/p/11521934.html
Recommended