第八周作业
/**
* 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 pointA = headA;
ListNode pointB = headB;
while(pointA == null || pointB == null || pointA.val != pointB.val) {
if(pointA == null) {
pointA = headB;
} else {
pointA = pointA.next;
}
if(pointB == null) {
pointB = headA;
} else {
pointB = pointB.next;
}
}
return pointA;
}
}
双指针法
时间复杂度 O(m+n)
空间复杂度 O(1)
评论