标签:else 思路 style pac mon 描述 struct null ret
1 /* 2 struct ListNode { 3 int val; 4 struct ListNode *next; 5 ListNode(int x) : 6 val(x), next(NULL) { 7 } 8 };*/ 9 class Solution { 10 public: 11 ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) { 12 unsigned int length1 = GetListLength(pHead1); 13 unsigned int length2 = GetListLength(pHead2); 14 15 int nLength = 0; 16 ListNode *pLong; 17 ListNode *pShort; 18 if(length1 > length2) 19 { 20 nLength = length1 - length2; 21 pLong = pHead1; 22 pShort = pHead2; 23 } 24 else 25 { 26 nLength = length2 - length1; 27 pLong = pHead2; 28 pShort = pHead1; 29 } 30 31 for(int i=0;i<nLength;i++) 32 { 33 pLong = pLong->next; 34 } 35 //两链表已对齐 36 while((pLong != NULL) && (pShort != NULL) && (pLong->val != pShort->val)) 37 { 38 pLong = pLong->next; 39 pShort = pShort->next; 40 } 41 ListNode *pFirstCommmonNode = pLong; 42 return pFirstCommmonNode; 43 44 } 45 unsigned int GetListLength(ListNode *pHead) 46 { 47 unsigned int length = 0; 48 ListNode *pNode = pHead; 49 while(pNode != NULL) 50 { 51 length++; 52 pNode = pNode->next; 53 } 54 return length; 55 } 56 };
标签:else 思路 style pac mon 描述 struct null ret
原文地址:http://www.cnblogs.com/qqky/p/6984638.html