当前位置: 首页 > news >正文

<02.26>Leetcode

 

 

 

想想为什么要a走b的路b走a的路 因为这样到达相交点的话是同时的(路程才是一样的)

这样能保证第二圈就相遇了 如果各走各的路很难相遇

两个无交点的链表也有一个null交点 

/**
 * 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 A = headA;
        ListNode B = headB;
        while(A!=B){
            A = (A != null?A.next:headB);
            B = (B != null?B.next:headA);
        }
        return A;
    }
}
//各走各的路
/**
 * 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 A = headA;
        ListNode B = headB;
        int flag = 0;
        while(A!=B){
            A = (A != null?A.next:headA);
            B = (B != null?B.next:headB);
            if(A == B && B == null){
                flag ++;
                if(flag == 2)return A;
                A = (A != null?A.next:headA);
                B = (B != null?B.next:headB);
            }
        }
        return A;
    }
}

 

 

/**
 * 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 reverseList(ListNode head) {
        ListNode cur = head,pre = null;
        while(cur!= null){
            ListNode tmp = cur.next;
            cur.next = pre;//修改next引用指向
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
}

 

/**
 * 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 reverseList(ListNode head) {
        return recur(head,null);//递归调用并返回    
    }
    private ListNode recur(ListNode cur,ListNode pre){
        if(cur == null)return pre;//进到这个地方了 然后开始返回
        ListNode res = recur(cur.next,cur);//递归后面的结点
        cur.next = pre;
        return res;
    }
}

 

 

 

/**
 * 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 boolean isPalindrome(ListNode head) {
        //如果链表为空或只有一个结点 就直接返回true
        if(head==null||head.next == null)return true;
        //找到前半部分链表的尾节点
        ListNode l_End = endOfFirstHalf(head);
        //反转后半部分链表并找到开头
        ListNode r_Head = reverseList(l_End.next);

        //判断是否会问
        ListNode p1= head;
        ListNode p2 = r_Head;
        boolean result = true;
        while(result==true&&p2 != null){
            if(p1.val!=p2.val)return false;
            p1 = p1.next;
            p2 = p2.next;
        }
        // 还原链表并返回结果
        l_End.next = reverseList(r_Head);
        return result;

    }
    private ListNode reverseList(ListNode head){
        ListNode pre = null;
        ListNode cur = head;
        while(cur != null){
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        } 
        return pre;//返回反转后链表的head
    }
    private ListNode endOfFirstHalf(ListNode head){
        ListNode fast = head;
        ListNode slow = head;
        while(fast.next!=null&&fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }//慢的走一步 快的走两步
}

 

 

public class Solution {
    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode slow = head;
        ListNode fast = head.next;//防止上来就相等
        while (fast != null && fast.next != null) {//就保证了fast.next.next存在
            if (slow == fast) {
                return true;
            }
            slow = slow.next; // 慢指针每次移动一步
            fast = fast.next.next; // 快指针每次移动两步
        }
        return false; // 如果快指针到达链表末尾,则没有环
    }
}
public class Solution {
    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {//就保证了fast.next.next存在
            
            slow = slow.next; // 慢指针每次移动一步
            fast = fast.next.next; // 快指针每次移动两步
            if (slow == fast) {
                return true;
            }
        }
        return false; // 如果快指针到达链表末尾,则没有环
    }
}

 

 

 

慢指针一定还没有走完一圈 

a = (k-1)(b+c) + c   即a == c会在入口处相遇

import java.util.HashSet;
import java.util.Set;

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

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null) {//判断这个就行了 不要多判断
            return null;
        }

        ListNode slow = head;
        ListNode fast = head;

        while (fast != null && fast.next != null) {//判断这个就行了
            slow = slow.next;
            fast = fast.next.next;

            if (slow == fast) {
                ListNode slow2 = head;
                while (slow != slow2) {//不要写while true
                    slow = slow.next;
                    slow2 = slow2.next;
                }
                return slow;
            }
        }

        return null;
    }
}

 

/**
 * 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 mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode new_head = new ListNode(-101);
        ListNode cur = new_head;
        while(list1!=null&&list2!=null){
            if(list1.val<list2.val){
                cur.next = list1;
                list1 = list1.next;
            }else{
                cur.next = list2;
                list2 = list2.next;
            }
            cur = cur.next;
        }
        // 将剩余的节点连接到合并后的链表末尾
        cur.next = (list1 != null) ? list1 : list2;
        //想想为什么这里只用做一个 而不用将剩下的每个节点都做 因为链表存的是指针
        return new_head.next;
    }
}

 

 

http://www.dtcms.com/a/40330.html

相关文章:

  • 力扣LeetCode:1472 设计浏览器历史记录
  • 驭势科技9周年:怀揣理想,踏浪前行
  • 《Linux系统编程篇》共享内存(Linux 进程间通信(IPC))——基础篇
  • 在 Vue 组件中,如何确认父组件在 add 模式下传入 value 的情况及其对子组件 getProducts() 方法的触发影响?
  • matlab 七自由度车辆模型轮毂电机驱动电动汽车的振动分析
  • 【01】Cocos游戏开发引擎从0开发一款游戏-cocos环境搭建以及配置-Cocos Creator软件系统下载安装-node环境-优雅草卓伊凡
  • Python Cookbook-2.14 回退输入文件到起点
  • 【Linux】进程优先级 | 进程调度(三)
  • TCP/IP 5层协议簇:物理层
  • 软件工程----敏捷模型
  • 医疗UI的特殊法则:复杂数据可视化的“零错误”设计守则
  • MySQL 存储过程详解
  • 3D线上艺术展览:开启未来艺术观赏新纪元
  • 【 .sdL】具体解析方法思路和步骤
  • AGV入门基础问答
  • threeJs+vue 轻松切换几何体贴图
  • 【JAVA-数据结构】Map和Set
  • 2025-02-26 学习记录--C/C++-C语言 判断字符串S2是否在字符串S1中
  • 编写Redis开机自启动脚本
  • 《CentOS Stream 9 阿里云 yum 源修改:解锁系统更新新速度》
  • Unity百游修炼(3)——Tank_Battle(双人对战)详细制作全流程
  • Vue纷争:DefineProps 的作用是否因为DefineModel而减弱
  • 日语学习-日语知识点小记-构建基础-JLPT-N4N5阶段(12):普通(ふつう)形 :变化方式 :日常朋友家人之间对话
  • 基于SpringBoot + Spring AI + Vue的全栈AI对话系统开发全流程解析
  • 使用uniapp内置组件webview消息传递
  • Zookeeper(67) Zookeeper在HBase中的应用是什么?
  • MySQL进阶04-MVCC实现原理
  • Unity中动态切换光照贴图的方法
  • 京准电钟解读:为何不能用网络上的NTP时间源服务器
  • leetcode 1472. 设计浏览器历史记录 中等