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

LeetCode:24. 两两交换链表中的节点 - 力扣(LeetCode)

思路:第一步:两两交换要考虑循环什么时候退出,当cur指针.next是null是就到尾部了,同理,链表不是奇数就是偶数,cur.next.next是空也是。

   第二步 循环条件判断完了接下来要实现交换,如图所示,按步骤来就好,提前将1,2,3存好,接下来按图走就行。

   最后一步 是cur如何向前去呢,将链表分成三个三个的形式来走循环,最后一步将变量指针cur=3即可

public ListNode swapPairs(ListNode head) {
            ListNode dummyhead = new ListNode(-1);
            dummyhead.next = head;
            ListNode cur = dummyhead;
            ListNode temp ;
            ListNode firstnode ;
            ListNode secondnode ;
            while (cur.next != null && cur.next.next != null){
                temp = cur.next.next.next;
                firstnode = cur.next;
                secondnode = cur.next.next;
                cur.next = secondnode;
                secondnode.next = firstnode;
                firstnode.next = temp;
                cur = firstnode;
            }
            return dummyhead.next;
    }

LeetCode:19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

思路:美妙的思路,快慢指针隔n+1个结点,+1是因为要想找到删除的结点需要找到该节点的上一个结点才行。

ListNode dummyhead = new ListNode(-1);
            dummyhead.next = head;
            ListNode falstnode = dummyhead;
            //这里for是先将快指针送到第n结点的位置
            for(int i = 0;i<n+1;i++){
                falstnode = falstnode.next;
            }
            //慢指针和快指针同步,当快指针为null时结束。说明到头了
            ListNode slownode = dummyhead;
            while( falstnode != null){
                falstnode = falstnode.next;
                slownode = slownode.next;
            }
            
            slownode.next =slownode.next.next;
            return dummyhead.next;

LeetCode:160. 相交链表 - 力扣(LeetCode)

leetcode 最浪漫的一题

思路:简单来说相交链表,两个指针走的路是原本x+y+z,走到尽头见不到你,于是走过你来时的路,等到相遇时才发现,你也走过我来时的路

相交链表最重要的就是这个思路:当链表到尾部时,将尾部替换成另一个链表的头结点,另一条也是如此,这样,原本两个相交的链表的头指针走过的路就是2+3+3 == 3+3+2 !! wc

//指针为null不能等于布尔类型的false。需要加一个cur != null、

 代码如下

        ListNode curB = headB;
        while(curA != curB){
            if(curA.next == null && curB.next == null){
                return  null;
            }
            if(curA.next != null){
                curA =curA.next;
            }else
            {
                curA = headB;
            }
            if(curB.next != null){
                curB =curB.next;
            }else
            {
                curB = headA;
            }
        }
        return curA;

LeetCode:142. 环形链表 II - 力扣(LeetCode)

思路:第一步判断链表有没有环存在,用快慢指针来判断,之间隔一步即可,若有环,快慢指针会相遇;

    那循环什么时候终止呢?当然是按照快指针来判断,因为要每次走两个,所以两个都要判断,当两个都是空时,说明到尾部了,循环该停止了。

   第二步 判断环点在什么位置,也是两个指针,一个头结点指针和一个相遇点指针,用数学知识可得,头结点指针和相遇点指针同时进行。最后一定会再次回到相遇点,那时候的相遇点指针指向的就是环的结点。

    

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;

 

posted @ 2024-02-26 21:19  22软工冷薄  阅读(1)  评论(0编辑  收藏  举报