leetCode 21.Merge Two Sorted Lists (合并排序链表) 解题思路和方法

原创 2015年07月06日 19:19:47
Merge Two Sorted Lists 

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.


思路:对两个已排序的单链表合并。算法上比较简单,与归并排序类似。只是数据结构上以前学的,现在忘的比较多,写第一遍的时候比较费力。而且想把重复代码写出方法,但是方法怎么都不能同步改变参数的值,没办法,第一遍力求准确,先A过再说。

代码有点乱,不过理解还是比较方便。如下:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head = null;
        ListNode p = null;
        
        //从小到大的顺序合并链表
        while(l1 != null && l2 != null){
            if(l1.val > l2.val){
            	if(head == null){
            		head = p = l2;
            	}else{
            		p.next = l2;
            		p = p.next;
            	}
            	l2 = l2.next;
            }else{
            	if(head == null){
            		head = p = l1;
            	}else{
            		p.next = l1;
            		p = p.next;
            	}
            	l1 = l1.next;
            }
        }
        //如果链表中还有数据,继续合并,以下最多只有一个链表还有数据
        while(l1 != null){
        	if(head == null){
        		head = p = l1;
        	}else{
        		p.next = l1;
        		p = p.next;
        	}
        	l1 = l1.next;
        }
        while(l2 != null){
        	if(head == null){
        		head = p = l2;
        	}else{
        		p.next = l2;
        		p = p.next;
        	}
        	l2 = l2.next;
        }
        p = null;
        return head;
    }
}

顺利A过之后考虑精简代码,因为上面的代码看着实在不舒服,网上参考一些资料,受启发后写出如下代码。

PS:两个代码的效果都差不多。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null || l2 == null)
            return l1 == null ? l2:l1;
        ListNode head = new ListNode(0);//定义一个头结点
        ListNode p = head;
        ListNode temp = null;
        
        while(l1 != null && l2 != null){
            if(l1.val > l2.val){
                temp = l2;//用一个temp保存现在的l1或l2
                l2 = l2.next;//l1或l2指针后移1位
            }else{
                temp = l1;
                l1 = l1.next;
            }//交换数据
            p.next = temp;
            p = p.next;
        }//temp取不为空的一个(也可能全为空)
        temp = l1 == null ?l2:l1;
        p.next = temp;//将剩余的全部链接即可(上面的方法太啰嗦了)
        return head.next;
    }
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetCode 23. Merge k Sorted Lists (合并k个排序链表) 解题思路和方法

Merge k Sorted Lists  Merge k sorted linked lists and return it as one sorted list. Analyze and des...

Leetcode #21 Merge Two Sorted Lists 合并有序链表 解题报告

0 题外话嗯,从这次开始我就把题目换一下,改成解题报告了,因为我小节小结总是不分,等有时间也去改下之前的解题报告咯~~我算了下自己的速度,作为一个本学期的额外爱好,到现在21天刷了100题,速度也还算...

LeetCode 21 Merge Two Sorted Lists(合并两个已排序的链表)(Linked List)

翻译合并两个排好序的链表,并返回这个新链表。 新链表应该由这两个链表的头部拼接而成。原文Merge two sorted linked lists and return it as a new lis...
  • NoMasp
  • NoMasp
  • 2015-11-10 20:38
  • 6451

Leetcode刷题记——21. Merge Two Sorted Lists(合并两有序链表)

一、题目叙述: Merge two sorted linked lists and return it as a new list. The new list should be...

leetcode_效率题解_[python/C++]_21. Merge Two Sorted Lists(合并2个有序链表)

相关题解: leetcode_效率题解_23. Merge k Sorted Lists(合并k个有序链表)题目链接 【题目】 Merge two sorted linked lists and...

leetcode_[python/C++]_21. Merge Two Sorted Lists(合并有序链表)

题目链接 【题目】 Merge two sorted linked lists and return it as a new list. The new list should be made b...

LeetCodet题解--21. Merge Two Sorted Lists(合并两个排序好的链表)

链接 LeetCode题目:https://leetcode.com/problems/merge-two-sorted-lists GitHub代码:https://github.co...

leetcode[21 链表]Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t...

LeetCode 21 Merge Two Sorted Lists 把两个链表有序连接

题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splic...

[leetcode-排序]--21. Merge Two Sorted Lists

Question 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)