public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
int size = 0;
while (node != null) {
node = node.next;
size++;
}
node.val = node.next.val;
node.next = node.next.next;
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int sizeA = 0, sizeB = 0;
ListNode ptrA = headA, ptrB = headB;
while (ptrA != null) {
ptrA = ptrA.next;
sizeA++;
}
while (ptrB != null) {
ptrB = ptrB.next;
sizeB++;
}
int diff = sizeA - sizeB;
if (diff > 0) {
while (diff-- > 0) {
headA = headA.next;
}
} else {
while (diff++ < 0) {
headB = headB.next;
}
}
while (headA != headB) {
headA = headA.next;
headB = headB.next;
}
return headA;
}