https://leetcode.com/problems/remove-linked-list-elements/
刪除鏈結串列中所有指定的數值。
一開始先用2個指標紀錄首節點和首節點的下一個,當先行的指標不等於要求的數值,把前一個指標連到現在指標然後移動現在指標至下一個指標,直到條件不符合為止。
struct ListNode* removeElements(struct ListNode* head, int val) {
while(head!=NULL && head->val == val)
head = head->next;
if(head == NULL)
return NULL;
struct ListNode * p = head->next;
struct ListNode * q = head;
while(p)
{
if(p->val != val)
{
q->next = p;
q = p;
}
else
{
q->next = NULL;
}
p = p->next;
}
return head;
}
var removeElements = function(head, val) {
while(head != null && head.val == val)
head = head.next;
if(head == null)
return null;
var post = head.next;
var cur = head;
while(post)
{
if(post.val != val)
{
cur.next = post;
cur = post;
}
else
{
cur.next = null;
}
post = post.next;
}
return head;
};
https://github.com/SIAOYUCHEN/leetcode
https://ithelp.ithome.com.tw/users/20100009/ironman/2500
https://ithelp.ithome.com.tw/users/20113393/ironman/2169
https://ithelp.ithome.com.tw/users/20107480/ironman/2435
https://ithelp.ithome.com.tw/users/20107195/ironman/2382
https://ithelp.ithome.com.tw/users/20119871/ironman/2210
https://ithelp.ithome.com.tw/users/20106426/ironman/2136
We are what we repeatedly do. Excellence, therefore, is not an act, but a habit.
我們每天做什麼,就會成為什麼樣的人。因此,卓越不是一種行為,而是一個習慣。