代码随想录算法训练营第四天

链表相关

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

参考文章
题目连接

思路

个人题解

class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummyPoint = new ListNode();
        dummyPoint.next = head;
        ListNode cur = dummyPoint;
        ListNode temp;
        ListNode first;
        ListNode second;

        while (cur.next != null && cur.next.next != null) {
            temp = cur.next.next.next;
            first = cur.next;
            second = cur.next.next;

            second.next = first;
            first.next = temp;
            cur.next = second;
            cur = first;

        }

        return dummyPoint.next;
    }
}

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

参考文章
题目连接

思路

个人题解

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyPoint = new ListNode();
        dummyPoint.next = head;

        ListNode slowPoint = dummyPoint;
        ListNode fastPoint = dummyPoint;
        for (int i = 0; i < n + 1; i++) {
            fastPoint = fastPoint.next;
        }
        while (fastPoint != null) {
            slowPoint = slowPoint.next;
            fastPoint = fastPoint.next;
        }
        slowPoint.next = slowPoint.next.next;

        return dummyPoint.next;
    }
}

面试题 02.07. 链表相交

参考文章
题目连接

思路

个人题解

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode A = headA;
        ListNode B = headB;
        int aLen = 0 , bLen = 0;
        while (A.next != null) {
            aLen++;
            A = A.next;
        }
        while (B.next != null) {
            bLen++;
            B = B.next;
        }
        
        A = headA;
        B = headB;
        
        int difLen = Math.abs(aLen - bLen);
        if (aLen > bLen) {
            for (int i = 0; i < difLen; i++) {
                A = A.next;
            }
        }else {
            for (int i = 0; i < difLen; i++) {
                B = B.next;
            }
        }

        while (A != null) {
            if (A == B) {
                return A;
            }
            A = A.next;
            B = B.next;
        }
        return null;
    }
}

面试题 02.07. 链表相交

参考文章
题目连接

思路

个人题解

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode fastPoint = head;
        ListNode slowPoint = head;

        while (fastPoint != null && fastPoint.next != null) {
            slowPoint = slowPoint.next;
            fastPoint = fastPoint.next.next;

            if (fastPoint == slowPoint) {
                fastPoint = head;
                while(fastPoint != slowPoint){
                    fastPoint = fastPoint.next;
                    slowPoint = slowPoint.next;
                }
                return fastPoint;
            }
        }
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值