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
  • 相关阅读:
    牛客IOI周赛17-提高组 卷积 生成函数 多项式求逆 数列通项公式
    6.3 省选模拟赛 Decompose 动态dp 树链剖分 set
    AtCoder Grand Contest 044 A Pay to Win 贪心
    5.29 省选模拟赛 树的染色 dp 最优性优化
    luogu P6097 子集卷积 FST FWT
    CF724C Ray Tracing 扩展欧几里得 平面展开
    5.30 省选模拟赛 方格操作 扫描线 特殊性质
    5.29 省选模拟赛 波波老师 SAM 线段树 单调队列 并查集
    Spring main方法中怎么调用Dao层和Service层的方法
    Bug -- WebService报错(两个类具有相同的 XML 类型名称 "{http://webService.com/}getPriceResponse"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。)
  • 原文地址:https://www.cnblogs.com/hf-cherish/p/4597259.html
Copyright © 2011-2022 走看看