leetcode203.移除链表元素
生活随笔
收集整理的這篇文章主要介紹了
leetcode203.移除链表元素
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題
刪除鏈表中等于給定值 val 的所有節點。
思路
刪除鏈表中某個結點的方法:node->next = node->next->next,可以刪除node的下一個結點;考慮到鏈表的頭結點也可能被刪除,所以使用dummyHead。
C++代碼實現
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:ListNode* removeElements(ListNode* head, int val) {ListNode* dummyHead = new ListNode(0);dummyHead->next = head;ListNode* cur = dummyHead;while(cur->next != NULL){if(cur->next->val == val){cur->next = cur->next->next;}elsecur = cur->next; }return dummyHead->next;} };轉載于:https://www.cnblogs.com/surimj/p/10446314.html
總結
以上是生活随笔為你收集整理的leetcode203.移除链表元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CNCF推出云原生网络功能(CNF)Te
- 下一篇: Json and Go