【LeetCode21】Merge Two Sorted Lists★

1.题目描述:

2.解题思路:

  本题是要合并两个已经有序的单链表,思路很简单,有两种方法:非递归和递归。

3.Java代码:

(1)非递归:

  为方便操作,定义一个辅助的头节点,然后比较原来两个链表的头节点,将小的那一个加入到合并链表,最后,当其中一个链表为空时,直接将另一个链表接入到合并链表即可。

 1 //public class LeetCode21 为测试
 2 public class LeetCode21 {
 3     public static void main(String[] args) {
 4         ListNode m1=new ListNode(1),m2=new ListNode(3),m3=new ListNode(5);
 5         m1.next=m2;
 6         m2.next=m3;
 7         System.out.println("链表1:"+m1.val+"->"+m2.val+"->"+m3.val);
 8         ListNode n1=new ListNode(2),n2=new ListNode(4),n3=new ListNode(6);
 9         n1.next=n2;
10         n2.next=n3;
11         System.out.println("链表2:"+n1.val+"->"+n2.val+"->"+n3.val);
12         ListNode result=new Solution().mergeTwoLists(m1, n1);
13         if(result!=null){
14             System.out.print("合并链表:"+result.val);
15             ListNode resultNext=result.next;
16             while(resultNext!=null){
17                  System.out.print("->"+resultNext.val);
18                  resultNext=resultNext.next;
19              }
20         }
21     }
22 }
23 class Solution {
24      public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
25          ListNode fakeHead=new ListNode(0);
26          ListNode p=fakeHead;
27          while(l1!=null&&l2!=null){
28              if(l1.val<l2.val){
29                  p.next=l1;
30                  l1=l1.next;
31              }else{
32                  p.next=l2;
33                  l2=l2.next;
34              }
35              p=p.next;
36          }
37          if(l1!=null) p.next=l1;
38          if(l2!=null) p.next=l2;
39          return fakeHead.next;
40         }
41 }
42 class ListNode {
43     int val;
44     ListNode next;
45     ListNode(int x) { val = x; }
46     }

测试结果:

(2)递归:

 1 //public class LeetCode21 为测试
 2 public class LeetCode21 {
 3     public static void main(String[] args) {
 4         ListNode m1=new ListNode(1),m2=new ListNode(3),m3=new ListNode(5);
 5         m1.next=m2;
 6         m2.next=m3;
 7         System.out.println("链表1:"+m1.val+"->"+m2.val+"->"+m3.val);
 8         ListNode n1=new ListNode(2),n2=new ListNode(4),n3=new ListNode(6);
 9         n1.next=n2;
10         n2.next=n3;
11         System.out.println("链表2:"+n1.val+"->"+n2.val+"->"+n3.val);
12         ListNode result=new Solution().mergeTwoLists(m1, n1);
13         if(result!=null){
14             System.out.print("合并链表:"+result.val);
15             ListNode resultNext=result.next;
16             while(resultNext!=null){
17                  System.out.print("->"+resultNext.val);
18                  resultNext=resultNext.next;
19              }
20         }
21     }
22 }
23 class Solution {
24      public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
25          if(l1==null) return l2;
26          if(l2==null) return l1;
27          if(l1.val<l2.val){
28              l1.next=mergeTwoLists(l1.next, l2);
29              return l1;
30          }else{
31              l2.next=mergeTwoLists(l1, l2.next);
32              return l2;
33          }
34         }
35 }
36 class ListNode {
37     int val;
38     ListNode next;
39     ListNode(int x) { val = x; }
40     }

测试结果:

  

posted @ 2017-03-02 23:50  追梦boy  阅读(205)  评论(0编辑  收藏  举报