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.


       首先拿到这道题目的时候,我觉得挺简单的,严蔚敏那本数据结构里面有这样的一个例题,我觉得不会很难,然后很快就写完了,但是这个题目中的链表和书中的链表稍有差别,书中有头指针,但是这里却没有头指针,有头指针的链表结构和没有头指针的链表差别在,有头指针的链表在插入删除时,所有的操作是一样的,相反,没有头指针的,第一个节点操作跟后面的节点是不一样的。


     
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        ListNode p,q,r,head;
        if(l1 == null && l2 == null){
           return null;
        }
         if(l1 == null){
             return l2;
        }
         if(l2 == null){
            return l1;
        }
        // if(p == null && q == null){
        //     return l1;
        // }
        p = l1;
        q = l2;
        head = null;
        if(p.val <= q.val){
               // r->next = p;
                head = p;
                p = p.next;
            }
        else{
                head = q;
                q = q.next;
                // r->next = q;
                // r-next = p;
            }
            r = head;
        while(p != null && q != null){
            if(p.val <= q.val){
               // r->next = p;
                r.next = p;
                r = p;
                p = p.next;
            }
            else{
                r.next = q;
                r = q;
                
                q = q.next;
                // r->next = q;
                // r-next = p;
                
            }
        }
        if(p == null){
            while(q != null){
                r.next = q;
                r = q;
                q = q.next;
            }
        }
        else{
            while(p != null){
                r.next = p;
                r = p;
                p = p.next;
            }
        }
        return head;
    }
}

终于体会到当初上数据结构时候,老师经常提醒的那句话,今天差点把我弄死了。写了半天,一直提示p和q未初始化。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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).
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值