码迷,mamicode.com
首页 > 编程语言 > 详细

[Leetcode][021] Merge Two Sorted Lists (Java)

时间:2015-05-07 07:33:44      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

题目在这里: https://leetcode.com/problems/merge-two-sorted-lists/

【标签】Linked List

【题目分析】这个题目就是merge sort在 Linked List中的变形。不多说,直接上代码了

 1   public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
 2         ListNode dummyHead = new ListNode(-1);
 3         ListNode node = dummyHead;
 4         while (l1 != null && l2 != null) {
 5             if (l1.val <= l2.val) {
 6                 node.next = l1;
 7                 l1 = l1.next;
 8             } else {
 9                 node.next = l2;
10                 l2 = l2.next;
11             }
12             node = node.next;
13         }
14         // append the remaining list
15         node.next = (l1 != null) ? l1 : l2;
16         return dummyHead.next;
17  }

 

[Leetcode][021] Merge Two Sorted Lists (Java)

标签:

原文地址:http://www.cnblogs.com/stevencooks/p/4483770.html

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