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

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

使用 虚拟头节点 可以有效解决该问题,剩下的只是模拟就好

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {

        if(head == null || head.next == null) return head;
        
        ListNode dummy = new ListNode(-1,head);
        ListNode p = dummy;
        ListNode q = head;
      //画图模拟,轻松写出
        while(p.next != null && q.next != null){
            p.next = q.next;
            p = p.next;
            q.next = p.next;
            p.next = q;
            p = p.next;
            q = q.next;
        }
        return dummy.next;
    }
}

###19. 删除链表的倒数第 N 个结点

  • 利用双指针,让快指针先走 n + 1
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        //新建一个虚拟头节点指向head
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;
        //快慢指针指向虚拟头节点
        ListNode fastIndex = dummyNode;
        ListNode slowIndex = dummyNode;

        // 只要快慢指针相差 n 个结点即可
        for (int i = 0; i <= n; i++) {
            fastIndex = fastIndex.next;
        }
        while (fastIndex != null) {
            fastIndex = fastIndex.next;
            slowIndex = slowIndex.next;
        }

        // 此时 slowIndex 的位置就是待删除元素的前一个位置。
        // 具体情况可自己画一个链表长度为 3 的图来模拟代码来理解
        // 检查 slowIndex.next 是否为 null,以避免空指针异常
        if (slowIndex.next != null) {
            slowIndex.next = slowIndex.next.next;
        }
        return dummyNode.next;
    }
}
  • 或者笨方法(我自己写的)先遍历一边,然后大体思路和快指针先走 n+1步一致
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head == null) return head;
        int size = 0;
        ListNode dummy = new ListNode(0,head);
        ListNode cur = dummy;
        while(cur!= null){
            size ++;
            cur = cur.next;
        }
        int count = size - n;
        ListNode pos = dummy;
        ListNode prev = dummy;
        while(count > 0){
            prev = pos;
            pos = pos.next;
            count --;
        }
        prev.next = pos.next;

        return pos == head? head.next : head;
    }
}

面试题 02.07. 链表相交

  • 让两个链表的尾部对齐,也就是先计算长度差值 n,长的先向前走 n 个节点
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int sizeA = 0;
        int sizeB = 0;
        ListNode p,q;
        while(p!=null){
            sizeA++;
            p = p.next;
        }
        p = headB;
        while(p!=null){
            sizeB++;
            p = p.next;
            }
        int gap = sizeA - sizeB > 0 ? sizeA - sizeB : sizeB - sizeA;
        int count = gap;
        if(sizeA > sizeB){
            p = headA;
            while(count > 0){
                p = p.next;
                count --;
            }
            q = headB;
            while(p != null && q != null){
                if(p == q)return q;
                p = p.next;
                q = q.next;
            }
        }else{
            p = headB;
            while(count > 0){
                p = p.next;
                count --;
            }
            q = headA;
            while(p != null && q != null){
                if(p == q)return q;
                p = p.next;
                q = q.next;
            }
        }
        return null;
    }
}

142. 环形链表 II

  • 可以直接用HashSet做,非常简单
  • 用指针做

我的思路,超限了

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head == null || head.next == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        ListNode pos = head;
        ListNode cur = null;
        
        //判断是否有环
        while(fast != null){
            fast = fast.next.next;
            if(fast == slow){
                cur = slow;
            }
            slow = slow.next;
        }
        //没有环 返回null
        if(cur == null) return null;

        //设置指针q 为 环中一个节点
        ListNode q = cur;
        //pos此时指向头节点,如果环中的一点是头节点,则直接返回
        if(pos == cur) return pos;
        //pos在每次cur循环一圈时 前进到下一个节点
        while( q != null){
            //此时 q 在环中运动,pos在q每次绕环一圈 运动一次
            q = q.next;
            //查看q和pos是否相交
            if(q == pos) return q;
            if(q == cur) pos = pos.next;
        }
        return q;
    }
}
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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值