码迷,mamicode.com
首页 > 其他好文 > 详细

Merge Two Sorted Lists

时间:2015-12-24 20:47:34      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

 1 /**
 2  * Definition for singly-linked list.
 3  * function ListNode(val) {
 4  *     this.val = val;
 5  *     this.next = null;
 6  * }
 7  */
 8 /**
 9  * @param {ListNode} l1
10  * @param {ListNode} l2
11  * @return {ListNode}
12  */
13 var mergeTwoLists = function(l1, l2) {
14     var ret = new ListNode(0),
15         p = ret;
16 
17     while (l1 !== null && l2 !== null) {
18         if (l1.val < l2.val) {
19             p.next = l1;
20             l1 = l1.next;
21         } else {
22             p.next = l2;
23             l2 = l2.next;
24         }
25 
26         p = p.next;
27     }
28 
29     if (l1 !== null) {
30         p.next = l1;
31     }
32 
33     if (l2 !== null) {
34         p.next = l2;
35     }
36 
37     return ret.next;
38 };

 

Merge Two Sorted Lists

标签:

原文地址:http://www.cnblogs.com/HuoAA/p/5074223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!