(链表)10-相交链表

 1 /*
 2 public class ListNode {
 3     int val;
 4     ListNode next = null;
 5 
 6     ListNode(int val) {
 7         this.val = val;
 8     }
 9 }*/
10 public class Solution {
11     public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
12         ListNode temp1 = pHead1;
13         ListNode temp2 = pHead2;
14         while(temp1 != temp2) {
15             temp1 = (temp1 == null) ? pHead2 : temp1.next;
16             temp2 = (temp2 == null) ? pHead1 : temp2.next;
17         }
18         return temp1;
19     }
20 }

 

posted @ 2023-11-17 22:47  StringBuilder  阅读(1)  评论(0编辑  收藏  举报