DAY4| 24. 两两交换链表中的节点 |19.删除链表的倒数第N个节点 |面试题 02.07. 链表相交 |142.环形链表II

重写了设计链表,注意LinkedList是java里面的专属类,不能用这个名字

/**
 * 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) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode cur = dummyHead;
        while(cur.next!=null&&cur.next.next!=null){
            //node结点数为奇数和偶数的判断条件
            ListNode firNode =cur.next;
            ListNode scNode = cur.next.next;
            ListNode temp = cur.next.next.next;
            cur.next = scNode;
            //忘记虚拟头节点了,这一步是最关键一步
            scNode.next = firNode;
            firNode.next = temp;
            cur = firNode;
        }
        return  dummyHead.next;
    }
}

很久没画图了,还是画个图理一下思路吧

画图可以用这个网站:

Excalidraw | Hand-drawn look & feel • Collaborative • Secure
在这里插入图片描述

奇数偶数的判定情况
交换次序

|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 dummyHead = new ListNode();
    dummyHead.next  =head;
    ListNode fast = dummyHead;
    ListNode slow = dummyHead;
    while(n>0){
        fast = fast.next;
        n--;
    }
    while(fast.next!=null){//detail
        fast = fast.next;
        slow = slow.next;
    }
    slow.next = slow.next.next;
    return dummyHead.next;
    }
}

用while不要用正向思维,用逆向思维,你想的是让fast.next==null

->while(fast.next!=null) 这样思路会清晰的多

在这里插入图片描述

面试题02.07链表相交

整体思路就是比对两个链表,我们可以发现

链表在交点之后的后半部分都是相同的

前半部分虽然不同,但是却在交点部分对齐了。

我们分别计算两个链表长度,得到差值

这个差值就是我们的操作指针在长链表中的移动距离

还有就是,兄弟们,注意这题测试用例

intersectVal = 8,
 listA = [4,1,8,4,5]
 listB = [5,0,1,8,4,5]
 skipA = 2
 skipB = 3

里面有个2,3,所以说他要求A链表相交前必须有2个结点,B必须有3个结点,不是地址问题。

如果题目没有这两个条件,8就是相交点

/**
 * 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 lenA =  0,lenB = 0;
        while(curA!=null){
            curA = curA.next;
            lenA ++;
        }
        while(curB!=null){
           curB  =curB.next;
           lenB++;
        }
        curA = headA;
        curB = headB;
        //我们固定认为lenA<lenB
        if(lenA>lenB){
          int temp = lenA;
          lenA = lenB;
          lenB = temp;
          ListNode temp2 = curA;
          curA = curB;
          curB = temp2;
        }
        int gap = lenB-lenA;
        while(gap-->0){
            curB = curB.next;
        }
        while(curA!=null){
            if(curA==curB){
                return curA;
            }
            else{
                curA = curA.next;
                curB = curB.next;
            }
        }
        return null;
    }
}

142.环形链表

在这里插入图片描述

我们的n是fast指针走的圈数,那么y+z相当于原点嘛,x=z

利用这个就可以写出下面的代码

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
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){
            ListNode index1 = fast;
            ListNode index2 = head;
            while(index1!=index2){
                index1 = index1.next;
                index2 = index2.next;
            }
            return index1;
            }
        }
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值