[LeetCode] 21. Merge Two Sorted Lists

Insert ordered a mixed list. To the two ordered list, place two lists merge, and the output of the list is ordered. The idea is simply to traverse the two lists, and then compare the node between the two. Directly on the code.

The length of time O (m + n), two lists of

Space O (1)

 1 /**
 2  * @param {ListNode} l1
 3  * @param {ListNode} l2
 4  * @return {ListNode}
 5  */
 6 var mergeTwoLists = function(l1, l2) {
 7     let dummy = new ListNode(0);
 8     let cur = dummy;
 9     while (l1 !== null && l2 !== null) {
10         if (l1.val < l2.val) {
11             cur.next = l1;
12             l1 = l1.next;
13         } ELSE {
 14              Cur.next = L2;
15              L2 = L2.next;
16          }
 17          Cur = Cur.next;
18      }
 19 the      if (L1! == Hotel Last Minute Deals ) {
 20          Cur.next = L1;
21      }
 22 the      if (L2! == Hotel Last Minute Deals ) {
 23          Cur.next = L2;
24      }
 25      Return Dummy.next;
26 };

 

Guess you like

Origin www.cnblogs.com/aaronliu1991/p/11828849.html