142. Linked List Cycle II - Leetcode

142. Linked List Cycle II

Problems:

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Note:

Do not modify the linked list.

Solution:

题目的意思是有个链表,其中一部分有循环。那么找出循环开始的那个点。
我们使用快慢指针就可以做到。
快指针:每次都移动两个点。
慢指针:每次都移动一个点.
假设循环链表如下:
这里写图片描述
这个链表是带有一个循环的。在节点3(绿色)是循环节点开始的节点。
那么我们可以计算出在节点6的时候。快慢指针刚好重合了。慢指针走了6个节点。快指针走了12个节点。
我们列一个公式。设节点1到节点3的距离为X,节点3到节点6的位置为Y。节点6到节点3的位置距离为Z。
刚刚慢指针走的距离是:X + Y
刚刚快指针走的距离是: X + Y + Z + Y
因为快指针每次走的距离都是慢指针的两倍,所以 2*(X + Y) = X + Y + Z + Y 于是 X = Z
由此可以得出结论,当找到快慢指针的汇聚点后,然后一个指针从头开始,一个指针从现在位置开始依次往后移动,最终汇聚的点就是循环链表循环开始的节点。

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head==null){
            return head;
        }
        ListNode slow = head;
        ListNode fast = head;
        while(fast!=null&&slow!=null){
            slow = slow.next;
            fast = fast.next;
            if(fast==null){
                break;
            }
            fast = fast.next;
            if(slow==fast){
                break;
            }
        }
        if(fast==null||slow==null){
            return null;
        }
        fast = head;
        while(fast!=slow){
            fast=fast.next;
            slow=slow.next;
        }
        return fast;
    }

}

更多Leecode问题解法可以参考我的Github仓库Leetcode学习解题

  • 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、付费专栏及课程。

余额充值