原题
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


- /**
- * 合并两个链表
- * @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;
- }
-
- }