原題
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;
}
}
算法實現類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上查看代碼片](http://static.javashuo.com/static/loading.gif)
![派生到個人代碼片](http://static.javashuo.com/static/loading.gif)
- /**
- * 合併兩個鏈表
- * @author zjn
- *
- */
- public class E17mergeSortedList {
- private static class ListNode{
- public int val;
- public ListNode next;
-
- public ListNode(int value){
- val = value;
- next = null;
- }
- }
-
- public static void main(String[] args){
- ListNode head = new ListNode(1);
- ListNode node1 = new ListNode(3);
- ListNode node2 = new ListNode(5);
- ListNode node3 = new ListNode(7);
-
- ListNode head3 = new ListNode(2);
- ListNode nodee1 = new ListNode(4);
- ListNode nodee2 = new ListNode(6);
- ListNode nodee3 = new ListNode(8);
-
- ListNode head1 = null;
-
- //ListNode head2 = head;
-
- head.next = node1;
- node1.next = node2;
- node2.next = node3;
- node3.next = null;
-
- head3.next = nodee1;
- nodee1.next = nodee2;
- nodee2.next = nodee3;
- nodee3.next = null;
-
- //head2.next = null;
-
- E17mergeSortedList example = new E17mergeSortedList();
- ListNode node11 = example.mergeSortList(head,head1);
- ListNode node = example.mergeSortList(head,head3);
- ListNode node22 = example.mergeSortList(head1,head1);
-
- System.out.println(node.val);
- System.out.println(node11.val);
- //System.out.println(node22.val);
-
- ListNode node44 = node11;
- while(node44 != null){
- System.out.println(node44.val);
- node44 = node44.next;
- }
-
- ListNode node33 = node;
- while(node33 != null){
- System.out.println(node33.val);
- node33 = node33.next;
- }
-
- }
-
- private ListNode mergeSortList(ListNode head, ListNode head1) {
- if(head == null && head1 == null){
- try {
- throw new Exception("Invalid Input");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- if(head == null){
- System.out.println("here");
- return head1;
- }
- if(head1 == null){
- System.out.println("here");
- return head;
- }
- ListNode node = head;
- ListNode node1 = head1;
- ListNode mergeHead = null;
- //ListNode node2 = mergeHead;
- if(node.val < node1.val){
- mergeHead = node;
- mergeHead.next = mergeSortList(node.next,node1);
- }else{
- mergeHead = node1;
- mergeHead.next = mergeSortList(node,node1.next);
- }
- return mergeHead;
- }
-
- }