/**
* 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) {
if (headA == null || headB == null) return null;
ListNode curA = headA, curB = headB;
while (curA != curB) {
curA = (curA == null) ? headB : curA.next;
curB = (curB == null) ? headA : curB.next;
// 这段代码在两个链表不相交的时候会死循环
// curA = (curA.next == null) ? headB : curA.next;
// curB = (curB.next == null) ? headA : curB.next;
}
return curA;
}
}
160. 相交链表
最新推荐文章于 2024-07-19 22:20:25 发布