Day4 | 链表基础 24. 两两交换链表中的节点 / 19. 删除链表的倒数第N个节点 / 面试题 02.07. 链表相交 / 142. 环形链表 II

Leetcode 24. 两两交换链表中的节点

题目链接:24. 两两交换链表中的节点

思路:

之前做过,立刻想到了递归。

解题:成功

代码实现

方法一:递归

class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode temp = head.next.next;
        ListNode p2 = head.next;
        head.next.next = head;
        head.next = swapPairs(temp);
        return p2;
    }
}

方法二

class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode cur = dummyHead;
        while(cur.next != null && cur.next.next != null){
            ListNode temp1 = cur.next;
            ListNode temp2 = cur.next.next.next;
            cur.next = cur.next.next;
            cur.next.next = temp1;
            temp1.next = temp2;
            cur = temp1;
        }
        return dummyHead.next;
    }
}

Leetcode 19. 删除链表的倒数第N个节点

题目链接:19. 删除链表的倒数第N个节点

思路:

经典双指针用法。之前做过,但是忘了。看了提示,才想起来这个方法。

解题:成功,但是看了leetcode的提示。

代码实现

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode slow = dummyHead;
        ListNode fast = head;
        while(n > 0){
            fast = fast.next;
            n--;
        }
        while(fast != null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummyHead.next;
    }
}

Leetcode 面试题 02.07. 链表相交

题目链接:面试题 02.07. 链表相交

思路:

做过基本一样的题。

解题:半成功

代码实现

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }
        ListNode p1 = headA;
        ListNode p2 = headB;
        while(p1 != p2){
            p1 = p1 == null ? headB : p1.next;
            p2 = p2 == null ? headA : p2.next;
        }
        return p1;
    }
}

Leetcode 142. 环形链表 II

题目链接:142. 环形链表 II

思路:

想到之前写过几次的龟兔赛跑

解题:失败

知识点:

  1. 如何判断链表中是否有环:快慢指针,从头结点出发,fast指针每次移动两个节点,slow指针每次移动一个节点,如果 fast 和 slow 指针在途中相遇 ,说明这个链表有环。之所以让快指针每次移动两格,是因为这样在环内的时候,快指针相对于慢指针每次追赶一格,它们就不可能会错过。
  2. 如何找到环的入口:找到快慢指针相遇的点后,设置一个指针指向链表头部,一个指针指向相遇的点,两个指针每次移动一格,遍历,直到二者相遇,相遇处就是环的入口。

代码实现

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

总结

  1. 虚拟头结点
  2. 双指针 / 快慢指针
  3. 环形链表

考察链表的操作其实就是考察指针的操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值