zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    深入浅出Blazor webassembly之程序配置
    深入浅出Blazor webassembly之通过CascadingValue组件实现向子级组件传值
    深入浅出Blazor webassembly之数据绑定写法
    深入浅出Blazor webassembly之浏览器WSAM性能测试
    重构的秘诀:消除重复,清晰意图
    在多数据源中对部分数据表使用shardingsphere进行分库分表
    logstach http input
    Addax 备忘
    WIN10下面0x00000bcb共享打印机无法连接怎么办?
    Calcite(二): 从list到tree的转换1
  • 原文地址:https://www.cnblogs.com/hf-cherish/p/4597259.html
Copyright © 2011-2022 走看看