21. Merge Two Sorted Lists 之詢問

我正在學習merge sort
以下是我的代碼

struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* t = head;
    while(l1 && l2) {
        if(l1->val > l2->val) {
            t->next = l2;
            l2 = l2->next;
        }
        else {
            t->next = l1;
            l1 = l1->next;
        }
        t = t->next;
    }
    
    t->next =l1 ? l1:l2;            //這邊有疑問
    t = head->next;
    free(head);
    return t;
}

我想詢問 t ->next =l1 ? l1:l2;
這行我了解是一個if els條件句 概念如下
t->next如果等於l1 那就 t->next=l1
t->next如果不等於l1 那就 t->next=l2

令我不解的是我自己有追蹤程式碼
假設l1=[1.2] l2=[3.4.5]

img

追完 l1發現
t停留在2, l1停留在null
接著想要把 t 連接過去 l2

但是執行 t ->next =l1 ? l1:l2; 時
t ->next =l1 是成true 還是false呢?
我知道應該是false這樣才會接到l2
但是不知道為何false。

因為t ->next =l1 不是成立的嗎?

請大家解答了感謝

一个等号就是赋值,又不是==,在那里怎么会参加运算。那边的意思是l1为真t next就等于l1否则等于l2