[問題] C語言片段求解
開發平台(Platform): (Ex: Win10, Linux, ...)
Win10
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
未知
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
C語言指標求解
餵入的資料(Input):
listA = [4,1,8,4,5], listB = [5,0,1,8,4,5]
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
/** * Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode ln;
struct ListNode *getIntersectionNode(ln *headA, ln *headB) {
int lenA = 0,lenB = 0;
for(ln* pA = headA; pA; pA = pA->next) lenA++;//問題在這
for(ln* pB = headB; pB; pB = pB->next) lenB++;//問題在這
for(int i = 0; i<lenA-lenB; i++) headA = headA->next;
for(int i = 0; i<lenB-lenA; i++) headB = headB->next;
while(headA != headB){
headA = headA->next;
headB = headB->next;
}
return headA;
}
補充說明(Supplement):
ln* pA = headA 這我可以理解,new一個Listnode
pA:以此為for條件是什麼原理?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.236.59.205
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1550902392.A.01E.html
推
02/23 14:27,
5年前
, 1F
02/23 14:27, 1F
→
02/23 14:42,
5年前
, 2F
02/23 14:42, 2F