代码随想录Day04 | 24.两两交换链表中的节点 、19.删除链表的倒数第N个节点、面试题 02.07、142.环形链表II

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



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 dummyNode = new ListNode();
        dummyNode.next = head;

        ListNode cur = dummyNode;

        while (cur.next != null && cur.next.next != null) {
            ListNode p = cur.next;
            ListNode q = cur.next.next;
            ListNode r = q.next;
            cur.next = q;
            q.next = p;
            p.next = r;

            //注意cur的赋值,一定要赋值到cur.next.next即p
            cur = p;
        }

        return dummyNode.next;
    }
}

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



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.next == null) {
            return null;
        }
        ListNode dummyNode = new ListNode();
        dummyNode.next = head;
        ListNode p = dummyNode;
        ListNode q = dummyNode;
        for (int i = 0; i < n; i++) {
            p = p.next;
        }
        while (p.next != null) {
            p = p.next;
            q = q.next;
        }
        q.next = q.next.next;


        return dummyNode.next;
    }
}

面试题 02.07. 链表相交



class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
        next = null;
    }
}


class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode p = headA;
        ListNode q = headB;
        int flag = 0;
        while (p.next != null || q.next != null) {
            if (p == q) {
                break;
            }

            if (p.next == null) {
                if (flag == 2) {
                    break;
                }

                //移动p放在后面,否则会出错
//                p = headB;
                flag++;
                // System.out.println("p = headB");
            }
            if (q.next == null) {
                if (flag == 2) {
                    break;
                }
//                q = headA;
                flag++;
                // System.out.println("q = headA");
            }
            p = p.next != null ? p.next : headB;
            // System.out.println("p = " + p.val);
            q = q.next != null ? q.next : headA;
            // System.out.println("q = " + q.val);
        }

        return p == q ? p : null;
    }
}

142.环形链表II


class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
        next = null;
    }
}

class Solution {
    public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null) {
            return null;
        }
        if (head.next == head) {
            return head;
        }
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;
        ListNode slow = head;
        ListNode fast = head;

        while (slow != null && fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {
                break;
            }
        }
        int cnt = 0;
        if (fast == null || fast.next == null) {
            return null;
        }

        //cur从head出发,直到与slow相会
        ListNode cur = head;
        while (cur != slow) {
            cur = cur.next;
            slow = slow.next;
        }
        return slow;

    }
}
  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值