2022年11月3日 星期四

[237] Delete Node in a Linked List

覺得這一題並不難?(thinking 圖)
歸在medium感覺有點意外!賺到一題的fu
(但彷彿妳的思考邏輯寫出來的code的確是把它變成medium了嗎XD)
原來可以用一個pointer 就完成!!!真是博大精深的codgin 世界啊~~~(遠目)
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
void deleteNode(struct ListNode* node) {
    while(node->next!=NULL)
    {
        node->val = node->next->val;
        if (node->next->next==NULL)
        {
            node->next=NULL;
            return;
        }
        node = node->next;
    }    
}
還有一種寫法是只要兩行就可以做完的;
就是它把next 一直指去next->next , 但最一開始的node 都沒有移定這樣. 
而我的寫法是node自己也一路往下移了. 以上兒~~~

沒有留言:

張貼留言