- address
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * struct ListNode *next;
- * };
- */
- struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
- int getlenth(struct ListNode *head){
- int len = 0;
- while(head !=NULL){
- len++;
- head = head->next;
- }
- return len;
- }
- int lenA = getlenth(headA);
- int lenB = getlenth(headB);
- if (lenA>lenB){
- int cha = lenA-lenB;
- while(cha>0){
- headA = headA->next;
- cha--;
- }
- lenA = lenB;
- }
- if (lenA<lenB){
- int cha = lenB-lenA;
- while(cha>0){
- headB = headB->next;
- cha--;
- }
- lenB = lenA;
- }
- // 至此, 两个头已经一样了
- while(headA && (headA !=headB)){
- headB = headB->next;
- headA = headA->next;
- }
- return headA;
- }
来源: http://www.bubuko.com/infodetail-3719231.html