代码随想录算法训练营第四天| LeetCode 142, 02.07, 19 和 24

文章介绍了如何使用快慢指针解决环形链表的问题,包括检测环形链表的交点,以及删除链表的倒数第N个节点和两两交换链表节点的操作。
摘要由CSDN通过智能技术生成

142. 环形链表 II

总结一下思路:

1. 设置快慢指针,一个走一步,一个走两步。

2. 相遇时慢指针回到链表初始位置

3. 两个指针此时个走一步,一直循环到相遇

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

面试题 02.07. 链表相交

第一次看题目感觉很懵,不知道是要我干嘛。。。

1. 遍历两个链表,得出长度后回到链表开始的位置

2. 算出差值,哪个链表长,哪个就从开始位置先走相差的步数

3. 遍历两个链表,直到节点地址相等时return

/**
 * 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) {
        ListNode curA = headA;
        ListNode curB = headB;
        int numA = 0;
        int numB = 0;

        while (curA != null){
            curA = curA.next;
            numA++;
        }

        while (curB != null) {
            curB = curB.next;
            numB++;
        }

        int gap;
        if (numA > numB) {
            gap = numA - numB;

            while (gap != 0){
                headA = headA.next;
                gap--;
            }

            while (headA != headB){
                headA = headA.next;
                headB = headB.next;
            }
            return headA;
        } else {
            gap = numB - numA;

            while (gap != 0){
                headB = headB.next;
                gap--;
            }

            while (headB != headA){
                headA = headA.next;
                headB = headB.next;
            }
            return headA;
        }
    }
}

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

/**
 * 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) {
        ListNode dummy = new ListNode(0, head);
        ListNode fast = head;
        ListNode slow = dummy;
        // 找到n-1位置的node
        for (int i=0; i<n ; i++){
            fast = fast.next;
        }
        // 
        while (fast != null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummy.next;
    }
}

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

总结一下需要注意有头节点的操作,所以我用了dummyHead

/**
 * 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) {
            return null;
        }

        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode cur = dummyHead;
        while (cur.next != null && cur.next.next != null) {
            ListNode temp = cur.next;
            ListNode temp2 = cur.next.next.next;
            cur.next = cur.next.next;
            cur.next.next = temp;
            cur.next.next.next = temp2;
            cur = cur.next.next;
        }
        return dummyHead.next;
    }
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值