is the number of nodes in the linked list referred by headA
.
is the number of nodes in the linked list referred by headB
.
令 itA
與 itB
分別以 headA
與 headB
為起點,遍歷兩個單向鏈結串列。
當 itA
走到 headA
所指之單向鏈結串列末端節點的下一個節點 (即 nullptr
) 時,跳到 headB
繼續遍歷 headB
所指之單向鏈結串。
當 itB
走到 headB
所指之單向鏈結串列末端節點的下一個節點 (即 nullptr
) 時,跳到 headA
繼續遍歷 headA
所指之單向鏈結串。
當 itA
與 itB
各自都把兩個單向鏈結串列遍歷過一遍之後,所走步數是一樣的。所以
headA
與 headB
所指向的單向鏈結串列有交點,則 itA
與 itB
在走過相同的步數後,會停在此兩鏈結串列的交點上。headA
與 headB
所指向的單向鏈結串列沒有交點,則 itA
與 itB
在走過相同的步數後,會停在此兩鏈結串列末端節點的下一個節點 (即 nullptr
)。
is the number of nodes in the linked list referred by headA
.
is the number of nodes in the linked list referred by headB
.