• merge two sorted lists


    1. Question

    合并两个有序链表,返回的新链表是通过拼接原来的两个链表得到

    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.

    2. Solution(O(m+n))

     考虑以下特殊情况:

    • 链表为空:都为空,某个为空
    • 两链表排序方式不同

    为了方便处理,new一个空节点作为结果链表的头,其指向l1,然后再将l2合并到该链表中

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        //true is non-descending, false is descending
        public boolean compare( int a, int b ){
            if( a>b )    return false;
            return true;
        }
        
        public ListNode reverseList( ListNode l ){
            if( l==null || l.next==null )    return l;
            ListNode p = l;
            ListNode q = l.next;
            l.next = null;
            do{
                ListNode next = q.next;
                q.next = p;
                p = q;
                q = next;            
            }while( q!=null);
            return p;
        }
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            if( l1==null )    return l2;
            if( l2==null )    return l1;        
            
            //if the order of the two lists is different, reverse List l2
            if( l1.next!=null && l2.next!=null && compare(l1.val, l1.next.val) != compare(l2.val,l2.next.val) )
                l2 = reverseList( l2 );
            
            //if the order of the two lists is same
            boolean order;
            if( l1.next!=null ) order = compare(l1.val,l1.next.val );
            else if( l2.next != null )    order = compare( l2.val, l2.next.val );
            else order = true;
            // p is the present end pointer of the result list, l2 is the present inserting pointer of list l2.
            ListNode p =new ListNode(0);
            p.next = l1;
            ListNode res = p;
            do{
                if( compare(p.next.val,l2.val) == order )
                    p = p.next;
                else{
                    ListNode q = l2;
                    for( ; q.next!=null && compare(p.next.val, q.next.val)!=order; q=q.next );
                    ListNode temp = p.next;
                    p.next = l2;
                    l2 = q.next;
                    q.next = temp;
                    p = temp;
                }
            }while( p.next!=null && l2!=null );
            
            if( l2!=null )
                p.next = l2;
            return res.next;
        }
    }
    View Code

     上述代码可以改进,即批量插入l2的数据(之前实现了批量插入l1的数据)

     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) { val = x; }
     7  * }
     8  */
     9 public class Solution {
    10     public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
    11         if( list1 == null )
    12             return list2;
    13         ListNode res = new ListNode(0);
    14         res.next = list1;
    15         ListNode p = res;
    16         ListNode q = list2;
    17         for(  ;p.next!=null && q!=null; ){
    18             for( ; p.next != null && p.next.val <= q.val; p = p.next );
    19             ListNode originPNext = p.next;
    20             p.next = q;
    21             if( originPNext == null )    break;
    22             for( ; q.next != null && originPNext.val > q.next.val; q = q.next );
    23             ListNode originQNext = q.next;
    24             q.next = originPNext;
    25             if( originQNext == null )    break;
    26             p = q;
    27             q = originQNext;            
    28         }
    29         if( p.next == null )
    30             p.next = q;
    31         return res.next;
    32     }
    33 }
    View Code
  • 相关阅读:
    使用别名(CName)记录免费将顶级域名解析到动态IP上
    DataTable 排序
    VS2005中使用MySQL 5.0
    让VS2005用起来更顺手
    PowerBuilder 9.0 Datawindow 导出 pdf 文件
    使用WebClient自动填写并提交ASP.NET页面表单的源代码
    C#中使用条件运算符 (?:)
    如何申请 @msn.com 邮箱
    遇上你是我的缘[转]
    Linux也玩远程桌面(VNC)
  • 原文地址:https://www.cnblogs.com/hf-cherish/p/4597259.html
Copyright © 2020-2023  润新知