老网抑云了
题目
就是剑指offer里两个链表的公共节点
思路
a+c+b = b + c + a
具体代码
/**
* 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 PA = headA;
ListNode PB = headB;
while(PA != PB){
PA = PA == null? headB : PA.next;
PB = PB == null? headA : PB.next;
}
return PA;
}
}
来源leetcode热门题解,感觉真的太简洁了!