标签:adb val node turn div 查询 def loading lis
输入两个链表,找出它们的第一个公共节点。
如下面的两个链表:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: int GetNumsOfList(ListNode* headA) { int nums = 0; while (headA != NULL) { ++nums; headA = headA->next; } return nums; } ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { int numsA = GetNumsOfList(headA); int numsB = GetNumsOfList(headB); ListNode* B = headB; ListNode* A = headA; if (numsB > numsA) { int diff = numsB - numsA; while (diff --) { B = B->next; } } else if (numsA > numsB) { int diff = numsA - numsB; while (diff --) { A = A->next; } } while (A && B) { if (A==B) { return A; } A = A->next; B = B->next; } return NULL; } };
标签:adb val node turn div 查询 def loading lis
原文地址:https://www.cnblogs.com/morningsunlll/p/14533783.html