代码随想录打卡DAY4

两两交换链表节点

主要问题还是在于没有判断特殊情况,以及变量声明,像carl老师这种节点定义好并且注释的方法值得学习,我就只写了tmp,tmp1后面感觉写的时候知道是什么,再看就不知道了,最好不要怕浪费空间就first second一个个来

/**
 * 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;
        if(head == null || head.next == null) return head;
        ListNode cur=dummyhead;
        ListNode temp; // 临时节点,保存两个节点后面的节点
        ListNode firstnode; // 临时节点,保存两个节点之中的第一个节点
        ListNode secondnode; // 临时节点,保存两个节点之中的第二个节点
        while(cur.next!=null && cur.next.next != null){
            firstnode=cur.next;
            secondnode=cur.next.next;
            temp=cur.next.next.next;
            cur.next=secondnode;
            secondnode.next=firstnode;
            firstnode.next=temp;
            cur=firstnode;//调换12节点后指向3的节点应该是第一个节点
        }
        return dummyhead.next;  

       
    }
}

2.删除倒数第N个元素

还是要建立虚拟头结点,这样就不会出现一些越界的事情值得注意的是有了虚拟头结点后fast要走n+1步才能到第n个节点所以第一个for要取等,要删除的节点应该是第m-n个,所以原本是要走m-n+1步,但是我们要走到删除节点的前一个,所以就少走一步fast直接!=null就行不用fast.next!=null

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyhead=new ListNode(-1);
        dummyhead.next=head;
        ListNode fast=dummyhead;
        ListNode slow=dummyhead;
        for (int i=0;i<=n;i++){
            fast=fast.next;
        }
        while(fast!=null){
            slow=slow.next;
            fast=fast.next;
        }
        slow.next=slow.next.next;
        return dummyhead.next;
    }
}

3.链表相交

主要是判断链表个数时应该是当前链表不为空而不是slow.next不为空这种

/**
 * 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 fast=headA;
        ListNode slow=headB;
        int lenA=0;
        int lenB=0;
        int gap=0;
        while(fast!=null){
            lenA++;
            fast=fast.next;
        }
        while(slow!=null){
            slow=slow.next;
            lenB++;
        }
        ListNode curA=headA;
        ListNode curB=headB;
        if(lenA<lenB){
            int temp=lenA;
            lenA=lenB;
            lenB=temp;
            ListNode tempnode=headA;
            curA=curB;
            curB=tempnode;
        }
        gap=lenA-lenB;
        while(gap-->0){
            curA=curA.next;
        }
        while(curA!=null){
            if(curA==curB){
                return curA;
            }
            curA=curA.next;
            curB=curB.next;
        }
        return null;
        

}
}

4.环形链表

主要关键点在于fast走两步 slow走一步 相遇代表存在环

然后根据

相遇时: slow指针走过的节点数为: x + y, fast指针走过的节点数:x + y + n (y + z),n为fast指针在环内走了n圈才遇到slow指针, (y+z)为 一圈内节点的个数A。

因为fast指针是一步走两个节点,slow指针一步走一个节点, 所以 fast指针走过的节点数 = slow指针走过的节点数 * 2:

(x + y) * 2 = x + y + n (y + z)

两边消掉一个(x+y): x + y = n (y + z)

因为要找环形的入口,那么要求的是x,因为x表示 头结点到 环形入口节点的的距离。

所以要求x ,将x单独放在左面:x = n (y + z) - y ,

再从n(y+z)中提出一个 (y+z)来,整理公式之后为如下公式:x = (n - 1) (y + z) + z 注意这里n一定是大于等于1的,因为 fast指针至少要多走一圈才能相遇slow指针。

这个公式说明什么呢?

先拿n为1的情况来举例,意味着fast指针在环形里转了一圈之后,就遇到了 slow指针了。

当 n为1的时候,公式就化解为 x = z

这就意味着,从头结点出发一个指针,从相遇节点 也出发一个指针,这两个指针每次只走一个节点, 那么当这两个指针相遇的时候就是 环形入口的节点

也就是在相遇节点处,定义一个指针index1,在头结点处定一个指针index2。

让index1和index2同时移动,每次移动一个节点, 那么他们相遇的地方就是 环形入口的节点。

/**
 * 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;

        }
        
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值