本文共 1539 字,大约阅读时间需要 5 分钟。
1,没有初始化 CurA 和CurB
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { if (headA == NULL || headB == NULL) return NULL; int lenA = 0; int lenB = 0; ListNode *p = headA; while (p != NULL){ lenA ++; p = p->next; } p = headB; while (p != NULL){ lenB ++; p = p->next; } ListNode *curA = headA; ListNode *curB = headB; if (lenA > lenB){ int i = 0; while (i < lenA - lenB){ curA = curA->next; i ++; } }else if (lenB > lenA){ int i = 0; while (i < lenB - lenA){ curB = curB->next; i ++; } } while (curA != curB){ curA = curA->next; curB = curB->next; } return curA; }};
转载地址:http://lcpbb.baihongyu.com/