merge two sorted lists, 合并两个有序序列

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

 

posted @ 2016-05-30 16:31  32ddd  阅读(185)  评论(0编辑  收藏  举报