DAY4| 24. 两两交换链表中的节点、19.删除链表的倒数第N个节点、面试题 02.07. 链表相交、142.环形链表II

时间:2025-03-29 07:14:15
  • public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
  • if(headA==null || headB==null) return null;
  • ListNode curA = headA;
  • ListNode curB = headB;
  • int countA = 0;
  • int countB = 0;
  • while(curA!=null){
  • countA++;
  • curA = ;
  • }
  • while(curB!=null){
  • countB++;
  • curB = ;
  • }
  • int sub=0;
  • if(countA>=countB){
  • sub = countA-countB;
  • }else{
  • ListNode temp = headA;
  • headA = headB;
  • headB = temp;
  • sub = countB-countA;
  • }
  • curA = headA;
  • curB = headB;
  • while(sub!=0){
  • headA = ;
  • sub--;
  • }
  • while(headA!=null){
  • if(headA==headB) return headA;
  • headA = ;
  • headB = ;
  • }
  • return null;
  • }