代码随想录day4

两两交换链表中结点

使用虚拟头节点的方法进行更改,写代码的时候如果想不明白或者怕错记得画图

class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode cur = dummyHead;
        while (cur.next != null && cur.next.next != null) {
            ListNode tmp = cur.next;
            ListNode tmp1 = cur.next.next.next;
            cur.next = cur.next.next;
            cur.next.next = tmp;
            cur.next.next.next = tmp1;

            cur = cur.next.next;
        }
        return dummyHead.next;
    }
}

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

这道题最开始想了半天也没想到怎么用一趟扫描实现,然后去看了题解。
原来可以用这么巧妙的方法实现。

ps:如果日后回顾时发现忘记怎么实现了,记得先画图看看。

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

        for (int i = 1; i <= n + 1; i++) {
            fast = fast.next;
        }

        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }

        slow.next = slow.next.next;
        return dummyHead.next;
    }
}

链表相交

这题还算简单,先让两个链表末位对其然后从短的那个开始逐个结点比较找到相等的即可。

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int sizeA = 0, sizeB = 0;
        ListNode curA = headA;
        ListNode curB = headB;

        while (curA != null) {
            sizeA++;
            curA = curA.next;
        }
        while (curB != null) {
            sizeB++;
            curB = curB.next;
        }
        curA = headA;
        curB = headB;
        if (sizeA < sizeB) {
            int tmp = 0;
            tmp = sizeA;
            sizeA = sizeB;
            sizeB = tmp;

            ListNode t = new ListNode();
            t = curA;
            curA = curB;
            curB = t;
        }

        int size = sizeA - sizeB;
        for(int i = 0; i < size; i++) {
           curA = curA.next;
        }
        while(curA != null) {
            if (curA == curB) {
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        return null;        
    }
}

环形链表

读完题,第一反应直接遍历链表,顺便用HashSet存储结点,当遇到相同结点时证明有环,直接返回当前结点即可。不过这样写的话空间复杂度是O(N)。

public class Solution {
    public ListNode detectCycle(ListNode head) {
        HashSet<ListNode> set = new HashSet<>();
        while (head != null) {
            if(set.contains(head)) {
                return head;
            }
            set.add(head);
            head = head.next;
        }
        return null;
    }
}

看了视频之后一直在纠结一个点,就是为什么慢指针在环中一定是在第一圈就被追上了,后来仔细想想发现,如果当慢指针在环里走了一圈,那么快指针一定在环里走了两圈,而快指针又是每次比慢指针多走一个距离,也就是说慢指针走的这一圈中,快指针一定会追上慢指针,这时他们就相遇了。

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

            if (fast == slow) { //如果相遇 代表有环
                ListNode index1 = fast; 
                ListNode index2 = head; //丛头开始走的距离和快指针走的距离一样
                						//一定会在环的入口相遇
                while (index1 != index2) {
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index1;
            }
        }
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值