c++ - Can someone explain to me how this loop works within function only? -


studying linked lists , not sure how loop works assignment.

the code intended find intersection of 2 linked lists.

listnode *getintersectionnode(listnode *heada, listnode *headb) {     listnode *cur1 = heada, *cur2 = headb;     while(cur1 != cur2){         cur1 = cur1 ? cur1->next : headb;         cur2 = cur2 ? cur2->next : heada;     }     return cur1; } 

i'm not sure how cur1 = cur1 evaluated assignments rather boolean condition. understand how iteration works unsure of why can't do:

while(cur1 != cur2){     cur1 = cur1->next;     cur2 = cur2->next; } return cur1; 

with i'm pretty sure end runtime error though.

cur1 = cur1?cur1->next:headb; 

is ternary operator. checking if cur1 null before trying "move on next node" equivalent to:

if (cur1 != null){cur1 = cur1->next;} else {cur1 = headb;} 

the issue version proposed if cur1 happens null, segmentation fault.


Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -