国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

203. Remove Linked List Elements

2019-11-08 02:19:44
字體:
來源:轉載
供稿:網友

題目

Remove all elements from a linked list of integers that have value val.

Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5

Credits: Special thanks to @mithmatt for adding this PRoblem and creating all test cases.

Subscribe to see which companies asked this question.


思路

遍歷鏈表刪除對應節點,注意刪除首節點,尾節點,最后刪除后為NULL的場景


代碼

/** * 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) { if(head == NULL) { return NULL; } ListNode* tempHead = new ListNode(0); tempHead->next = head; ListNode* tempNode = tempHead; ListNode* freeNode; while(tempNode->next) { if(tempNode->next->val == val) { freeNode = tempNode->next; tempNode->next = freeNode->next; free(freeNode); continue; } tempNode = tempNode->next; } tempNode = tempHead->next; free(tempHead); return tempNode; }};
上一篇:nyoj 145 聰明的小珂

下一篇:方法引用

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌兰浩特市| 华容县| 喀什市| 永康市| 朝阳区| 三都| 沈丘县| 天台县| 高雄市| 绥德县| 玛沁县| 平塘县| 名山县| 突泉县| 乌兰县| 西安市| 无极县| 微山县| 镇远县| 罗定市| 云龙县| 天门市| 望城县| 司法| 侯马市| 郓城县| 申扎县| 原平市| 库车县| 浪卡子县| 大化| 驻马店市| 祁东县| 扎鲁特旗| 增城市| 宕昌县| 江口县| 桑植县| 微博| 肥东县| 尼玛县|