力扣:160. 相交链表(修改)
This commit is contained in:
parent
72884af511
commit
5f14194b4c
@ -7,7 +7,10 @@ import com.code.leet.entiy.ListNode;
|
||||
*/
|
||||
public class GetIntersectionNode {
|
||||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||
ListNode pA = headA, pB = headB, temp = null;
|
||||
ListNode pA = headA, pB = headB;
|
||||
if(pA == null || pB == null){
|
||||
return null;
|
||||
}
|
||||
while (pA != null || pB != null) {
|
||||
if (pA == null) {
|
||||
pA = headB;
|
||||
@ -15,15 +18,26 @@ public class GetIntersectionNode {
|
||||
if (pB == null) {
|
||||
pB = headA;
|
||||
}
|
||||
if (pA.val == pB.val && temp == null) {
|
||||
temp = pA;
|
||||
}
|
||||
if (pA.val != pB.val && temp != null) {
|
||||
temp = null;
|
||||
if (pA == pB) {
|
||||
return pA;
|
||||
}
|
||||
pA = pA.next;
|
||||
pB = pB.next;
|
||||
}
|
||||
return temp;
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ListNode headA = new ListNode(4);
|
||||
ListNode headB = new ListNode(5);
|
||||
ListNode node1A = new ListNode(1);
|
||||
ListNode node1B = new ListNode(1);
|
||||
ListNode node8 = new ListNode(4);
|
||||
|
||||
headA.next = node1A;
|
||||
node1A.next=node8;
|
||||
|
||||
headB.next=node1B;
|
||||
node1B.next=node8;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user