FTPrep, 21 Merge Two Sorted Lists

拿到手首先想到的就是所谓有的头删法+尾插法。因为是两个list同时头删,要判断 是否两个list都非空,如果都是非null才能循环进行。

就有while里面的条件判断。剩下的就是哪个还有就直接连上,代码如下:

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0);
        ListNode curr = dummy;
        while(l1!=null && l2!=null){
            if(l1.val<l2.val){
                curr.next=l1;
                curr=curr.next;
                l1=l1.next;
            }
            else{
                curr.next=l2;
                curr=curr.next;
                l2=l2.next;
            }
        }
        curr.next= (l1==null)?l2:l1; // 三目运算符号的:不能加表达式,只能加值哦啊,naiver~
		return dummy.next;
    }
}

以上是比较直白的思路,是个什么数据结构就这个结构的特点来。但是,这个只是表面上的特点,还没有把特点挖掘得很深刻。参考了递归的代码后,才觉得这个和链表就是一棵degenerated 的树,树中的递归算法用得很普遍,虽然不是最有效率的,但是晚上让人在思路上有点醒的感觉。

下面的代码不保证下次能直接bug free写出来,因为只是事后看一遍觉得很有道理的样子。还必须好好挖掘 递归的思路,递归代码的结构。

现在知道的递归代码的结构中必要的点:

1,判断好出口!!!这个至关重要。

2,把下一个node (list里就next,tree里是left,right,需要有条件判断),作为一个本函数的递归调用的点(或者叫做递出去的点,递出去的点就是对本身的调用!)。就是说我处理好了现在这个状态下的单独的一个node,下一个node,交给 SelfFunc(.next) 去处理。

代码如下:

/**
 * 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){
            return l2;
        }
        if(l2 == null){
            return l1;
        }
        
        ListNode mergeHead;
        if(l1.val < l2.val){
            mergeHead = l1;
            mergeHead.next = mergeTwoLists(l1.next, l2);
        }
        else{
            mergeHead = l2;
            mergeHead.next = mergeTwoLists(l1, l2.next);
        }
        return mergeHead;
    }
}

最后提一点,也是要摆脱的一个思维定势,或者说都过显现看本质。

树的root,或者链表的 l1, l2都是一个节点,仅仅是一个节点,不要把他认为是整个一棵树,或者整个一条链,因为树和链表归根结底都关于node的处理,实实在在的讲就是node。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To merge k sorted linked lists, one approach is to repeatedly merge two of the linked lists until all k lists have been merged into one. We can use a priority queue to keep track of the minimum element across all k linked lists at any given time. Here's the code to implement this idea: ``` struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // Custom comparator for the priority queue struct CompareNode { bool operator()(const ListNode* node1, const ListNode* node2) const { return node1->val > node2->val; } }; ListNode* mergeKLists(vector<ListNode*>& lists) { priority_queue<ListNode*, vector<ListNode*>, CompareNode> pq; for (ListNode* list : lists) { if (list) { pq.push(list); } } ListNode* dummy = new ListNode(-1); ListNode* curr = dummy; while (!pq.empty()) { ListNode* node = pq.top(); pq.pop(); curr->next = node; curr = curr->next; if (node->next) { pq.push(node->next); } } return dummy->next; } ``` We start by initializing a priority queue with all the head nodes of the k linked lists. We use a custom comparator that compares the values of two nodes and returns true if the first node's value is less than the second node's value. We then create a dummy node to serve as the head of the merged linked list, and a current node to keep track of the last node in the merged linked list. We repeatedly pop the minimum node from the priority queue and append it to the merged linked list. If the popped node has a next node, we push it onto the priority queue. Once the priority queue is empty, we return the head of the merged linked list. Note that this implementation has a time complexity of O(n log k), where n is the total number of nodes across all k linked lists, and a space complexity of O(k).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值