合併兩個排好序的單鏈表

原題

  Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.html

題目大意

  合併兩個排序鏈表並返回一個新的列表。新的鏈表的結果由原先的兩個鏈表結點組成,也就是不能合併後的鏈表不能包含新建立的結點。node

解題思路

  使用頭結點root進行輔助操做,建立一個頭結點,再使用兩個引用指向兩個鏈表的頭結點,將較小的結點值的結點摘下來接到root鏈表的末尾,同時被摘的鏈頭引用移動到下一個結點,一直操做,到到原先兩個鏈表中有一個爲空,最後再將剩下的結點接到root鏈表的末尾。最後返回root的下一個結點,其就爲新的鏈表頭。算法

代碼實現

鏈表結點類app

public class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
        next = null;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

算法實現類spa

public class Solution {

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {

        ListNode head = new ListNode(0); // 建立一個頭結點,最後還要刪除掉
        ListNode tail = head;

        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                tail.next = l1;
                l1 = l1.next;
            } else {
                tail.next = l2;
                l2 = l2.next;
            }

            tail = tail.next; // 移動到新的尾結點
        }

        tail.next = (l1 != null ? l1 : l2);

        return head.next; // head的下一個節點是第一個數據結點
    }
}

 

分析:首先這兩個鏈表都是遞增排序的且要求合併後的鏈表也是遞增排序的,思路很簡單採用遞歸比較兩個鏈表結點值大小並將較小的值加入到鏈表中.net

     代碼以下:code

 

[html] view plain copyhtm

在CODE上查看代碼片派生到個人代碼片

  1. /**  
  2.  * 合併兩個鏈表  
  3.  * @author zjn  
  4.  *  
  5.  */  
  6. public class E17mergeSortedList {  
  7.     private static class ListNode{  
  8.         public int val;  
  9.         public ListNode next;  
  10.           
  11.         public ListNode(int value){  
  12.             val = value;  
  13.             next = null;  
  14.         }  
  15.     }  
  16.       
  17.     public static void main(String[] args){  
  18.         ListNode head = new ListNode(1);  
  19.         ListNode node1 = new ListNode(3);  
  20.         ListNode node2 = new ListNode(5);  
  21.         ListNode node3 = new ListNode(7);  
  22.           
  23.         ListNode head3 = new ListNode(2);  
  24.         ListNode nodee1 = new ListNode(4);  
  25.         ListNode nodee2 = new ListNode(6);  
  26.         ListNode nodee3 = new ListNode(8);  
  27.           
  28.         ListNode head1 = null;  
  29.           
  30.         //ListNode head2 = head;  
  31.           
  32.         head.next = node1;  
  33.         node1.next = node2;  
  34.         node2.next = node3;  
  35.         node3.next = null;  
  36.           
  37.         head3.next = nodee1;  
  38.         nodee1.next = nodee2;  
  39.         nodee2.next = nodee3;  
  40.         nodee3.next = null;  
  41.           
  42.         //head2.next = null;  
  43.           
  44.         E17mergeSortedList example = new E17mergeSortedList();  
  45.         ListNode node11 = example.mergeSortList(head,head1);  
  46.         ListNode node = example.mergeSortList(head,head3);  
  47.         ListNode node22 = example.mergeSortList(head1,head1);  
  48.           
  49.         System.out.println(node.val);  
  50.         System.out.println(node11.val);  
  51.         //System.out.println(node22.val);  
  52.           
  53.         ListNode node44 = node11;  
  54.         while(node44 != null){  
  55.             System.out.println(node44.val);  
  56.             node44 = node44.next;  
  57.         }  
  58.           
  59.         ListNode node33 = node;  
  60.         while(node33 != null){  
  61.             System.out.println(node33.val);  
  62.             node33 = node33.next;  
  63.         }  
  64.           
  65.     }  
  66.   
  67.     private ListNode mergeSortList(ListNode head, ListNode head1) {  
  68.         if(head == null && head1 == null){  
  69.             try {  
  70.                 throw new Exception("Invalid Input");  
  71.             } catch (Exception e) {  
  72.                 // TODO Auto-generated catch block  
  73.                 e.printStackTrace();  
  74.             }  
  75.             return null;  
  76.         }  
  77.         if(head == null){  
  78.             System.out.println("here");  
  79.             return head1;  
  80.         }  
  81.         if(head1 == null){  
  82.             System.out.println("here");  
  83.             return head;  
  84.         }  
  85.         ListNode node = head;  
  86.         ListNode node1 = head1;  
  87.         ListNode mergeHead = null;  
  88.         //ListNode node2 = mergeHead;  
  89.             if(node.val < node1.val){  
  90.                 mergeHead = node;  
  91.                 mergeHead.next = mergeSortList(node.next,node1);  
  92.             }else{  
  93.                 mergeHead = node1;  
  94.                 mergeHead.next = mergeSortList(node,node1.next);  
  95.             }  
  96.         return mergeHead;  
  97.     }  
  98.   
相關文章
相關標籤/搜索