算法Day4-两两交换链表节点,删除链表的倒数第N个节点,链表相交,环形链表

两两交换链表节点

题目:LeetCode24

链接:https://leetcode.cn/problems/swap-nodes-in-pairs/submissions/

在这里插入图片描述

​ 看到题目的第一感觉就是简单的两两交换链表元素,就是删除链表元素和插入链表元素的组合操作,再就是要考虑奇偶问题,这个很简单写个i%2判断一下分情况就好,有了前两天的经验,本题写的速度比之前有所提升,题解如下:

class Solution {
    public ListNode swapPairs(ListNode head) {
        int count = 0;
        ListNode top = new ListNode();
        ListNode cur = new ListNode();
        ListNode pre = new ListNode();
        ListNode temp = new ListNode();

        if(head!=null){
            pre = head;
            while(pre.next!=null){
                if(count%2==0){
                    cur = pre.next;
                    temp = cur;
                    top.next = cur;
                    pre.next = temp.next;
                    cur.next = pre;
                    count++;
                }else{
                    top = pre;
                    pre = pre.next;
                    count++;
                }
                if(count==1){
                    head = cur;
                }
            }

        }
        return head;
    }
}
删除链表的倒数第N个节点

题目:LeetCode19

链接:https://leetcode.cn/problems/remove-nth-node-from-end-of-list/

在这里插入图片描述

​ 看到题目的第一眼,感觉题目应该不是很难,就是可能用多一些存储空间,遍历两次就能做出来,于是写了代码,如下:

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        int x = 0;
        ListNode l1 = new ListNode();
        l1 = head;
        while(l1!=null){
            l1 = l1.next;
            x++;
        }
        l1 = head;
        if(n>x||n<=0)return head;
        if(n<x){
            for(int i = 0;i<x-n-1;i++){
                l1 = l1.next;
            }
            l1.next = l1.next.next;
        }
        if(n==x){
            l1 = l1.next;
            return l1;
        }
        return head;
    }
}

​ 在看题解的时候发现一个双指针的方法也不错,一起放进来提供多一些思路

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0, head);
        ListNode first = head;
        ListNode second = dummy;
        for (int i = 0; i < n; ++i) {
            first = first.next;
        }
        while (first != null) {
            first = first.next;
            second = second.next;
        }
        second.next = second.next.next;
        ListNode ans = dummy.next;
        return ans;
    }
}
链表相交

题目:LeetCode面试题02.07

链接:https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/

在这里插入图片描述

​ 初次看到题目想着如果链表的后半段是相同的,那就可以从后开始往前遍历,但是单链表要解决从后往前的问题,我想到的就是用链栈,所以就用栈实现了,代码如下:

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode dummy1 = new ListNode(0,headA);
        ListNode dummy2 = new ListNode(0,headB);
        Deque<ListNode> stack1 = new LinkedList<ListNode>();
        Deque<ListNode> stack2 = new LinkedList<ListNode>();
        ListNode x = new ListNode();
        ListNode y = new ListNode();
        ListNode temp = new ListNode();
        ListNode a = dummy1;
        ListNode b = dummy2;
        ListNode c = new ListNode();
        ListNode d = new ListNode();
        while(a!=null){
            stack1.push(a);
            if(a.next==null){
                c = a;
            }
            a = a.next;
        }
        while(b!=null){
            stack2.push(b);
            if(b.next==null){
                d = b;
            }
            b = b.next;
        }
        if(c!=d)return null;
        while(x!=null||y!=null){
            x = stack1.pop();
            y = stack2.pop();
            if(x!=y){
                return temp;
            }
            temp = x;
        }
        return null;
    }
}

​ 自己在想的时候也有想过能不能用java集合的特性不包含相同元素来写,但是没有去实现当时,看了下力扣的题解,确实有这个解法,也确实挺不错的,放在这里提供多一种思路

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<ListNode> visited = new HashSet<ListNode>();
        ListNode temp = headA;
        while (temp != null) {
            visited.add(temp);
            temp = temp.next;
        }
        temp = headB;
        while (temp != null) {
            if (visited.contains(temp)) {
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }
}
环形链表II

题目:LeetCode142

链接:https://leetcode.cn/problems/linked-list-cycle-ii/

在这里插入图片描述

​ 初看题目,考虑了一下最简单的方法也是最暴力的,就是用两个指针双循环看能不能遍历一遍,后面发现这方法好像行不通,找不到外圈循环的临界值。然后就是想了下用快慢指针,但是一开始觉得行不通,因为可能快指针会跳过慢的从而遍历不到,故想不出来

​ 在看了题解之后,发现了只需要让两个指针之间的相对速度控制在一即可,再有就是自己没有考虑到判断出链表有环之后的操作应该是怎么样的,视频中是用数学手段设置变量列方程解决的,大大提升了效率,这一点很值得学习,从前很少碰到这样的问题(也可能是我也没做多少题)。看了题解之后写出代码如下:

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head==null)return null;
        ListNode l1 = new ListNode();
        ListNode l2 = new ListNode();
        ListNode l3 = new ListNode();
        l1 = head;
        l2 = head;
        l3 = head;
        do {
            if(l1==null||l1.next==null)return null;
            l1 = l1.next.next;
            l2 = l2.next;
        }while(l1!=l2);
        while(l3!=l2){
            l2 = l2.next;
            l3 = l3.next;
        }
        return l3;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值