Ai
1 Star 0 Fork 0

wu-xjg/LeetCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Code160.java 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
wu-xjg 提交于 2025-02-18 22:18 +08:00 . day02
package Code;
import java.util.List;
public class Code160 {
static class ListNode{
int val;
ListNode next;
ListNode(int x){
val= x;
next = null;
}
}
static ListNode getIntersectionNode(ListNode headA, ListNode headB){
if(headA == null || headB == null){
return null;
}
ListNode pA = headA, pB = headB;
while(pA != pB){
pA = pA== null? headB : pA.next;
pB = pB== null? headA : pB.next;
}
return pA;
}
public static void main(String[] args) {
ListNode commonNode = new ListNode(8);
ListNode headA = new ListNode(4);
headA.next = new ListNode(1);
headA.next.next = commonNode;
commonNode.next = new ListNode(4);
commonNode.next.next = new ListNode(5);
ListNode headB = new ListNode(5);
headB.next = new ListNode(6);
headB.next.next = new ListNode(1);
headB.next.next.next = commonNode;
ListNode intersectionNode = getIntersectionNode(headA, headB);
if (intersectionNode != null) {
System.out.println("相交节点的值为: " + intersectionNode.val);
} else {
System.out.println("两个链表不相交");
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wu-xjg/leet-code.git
git@gitee.com:wu-xjg/leet-code.git
wu-xjg
leet-code
LeetCode
master

搜索帮助