每日三题-环形链表I、环形链表II、排序链表

👨‍💻个人主页: 才疏学浅的木子
🙇‍♂️ 本人也在学习阶段如若发现问题,请告知非常感谢 🙇‍♂️
📒 本文来自专栏: 算法
🌈 算法类型Hot100题 🌈
❤️ 支持我:👍点赞 🌹收藏 🤟关注

环形链表I

在这里插入图片描述

解法一

哈希集合
遍历链表并判断哈希集合里面是否包含当前节点
如果包含则存在环

public class Solution {
    public boolean hasCycle(ListNode head) {
        HashSet<ListNode> set = new HashSet<>();
        while(head != null){
            if(set.contains(head)) return true;
            else set.add(head);
            head = head.next;
        }
        return false;
    }
}

解法二

双指针
一个quick指针一个slow指针
quick每次走
slow每次走

假如没有环
那么quick就会为空,直接返回false
如果有环
那么quick回比slow先进入环,slow后进入环
他们总会在环里相遇所有quick == slow

public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null) return false;

        ListNode quick = head.next;
        ListNode slow = head;
        
        while(quick != slow){  // 不相等就一直循环
            if(quick == null ||  quick.next == null) return false; // 如果为空直接退出
            quick = quick.next.next;
            slow = slow.next;
        }
        return true;
    }
}

环形链表II

在这里插入图片描述

解法一

哈希集合
如果在该哈希集合中存在就返回当前节点

public class Solution {
    public ListNode detectCycle(ListNode head) {
        HashSet<ListNode> set = new HashSet<>();
        while(head != null){
            if(set.contains(head)) return head;
            else set.add(head);
            head = head.next;
        }
        return null;
    }
}

解法二

双指针
像环形链表1一样可以找到相遇节点
然后slow指向头节点,然后quick与slow每次向后移动一步直到相遇
在这里插入图片描述
所以下次相遇时候就是第一个节点

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

注意!!!

如果相遇那个方法是第一种的话,即quick = head.next
那么quick需要在相遇之后再向移动一个位置,推到如上

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

排序链表

在这里插入图片描述

解法一

使用优先队列
先循环一遍把当前节点放进去,再重构有序链表

class Solution {
    public ListNode sortList(ListNode head) {
        if(head == null || head.next == null) return head;
        PriorityQueue<ListNode> p = new PriorityQueue<>((l1,l2)->l1.val-l2.val);
        ListNode t = head;
        while(t != null){ // 遍历进优先队列
            p.add(t);
            t = t.next;
        }
        ListNode res = new ListNode();
        t = res;
        while(!p.isEmpty()){ // 重构
            ListNode node = p.poll();
            t.next = node;
            t = t.next;
        }
        t.next = null; // 避免出现环
        return res.next;
    }
}

解法二

归并排序

从顶到底

class Solution {
    public ListNode sortList(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode quick = head.next;
        ListNode slow = head;
        while(quick != null && quick.next != null){
            quick = quick.next.next;
            slow = slow.next;
        }
        ListNode mid = slow.next;
        slow.next = null; // 将一个链表断开为两个链表
        ListNode left = sortList(head);
        ListNode right = sortList(mid); 

        // 相当于两个有序链表的合并
        ListNode res = new ListNode();
        ListNode t = res;
        while(left != null && right != null){
            if(left.val < right.val){
                t.next = left;
                left = left.next;
            }else{
                t.next = right;
                right = right.next;
            }
            t =t.next;
        }
        if(left != null) t.next = left;
        if(right != null) t.next = right;
        return res.next;
    }
}
  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 17
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白羽uou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值