每日一练-leetcode
剑指 Offer 52. 两个链表的第一个公共节点
输入两个链表,找出它们的第一个公共节点。
此题与上次咱们做的思路是一样的可见前两次博客
/**
* 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) {
ListNode pre,cur;
pre = headA;
cur = headB;
while(pre != cur){
pre = pre == null?headB:pre.next;
cur = cur == null?headA:cur.next;
}
return pre;
}
}
浙公网安备 33010602011771号