代码拉取完成,页面将自动刷新
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("两个链表不相交");
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。