LintCode: Remove Linked List Elements
发布日期:2021-10-24 15:11:44 浏览次数:40 分类:技术文章

本文共 1154 字,大约阅读时间需要 3 分钟。

C++

1 /** 2  * Definition for singly-linked list. 3  * struct ListNode { 4  *     int val; 5  *     ListNode *next; 6  *     ListNode(int x) : val(x), next(NULL) {} 7  * }; 8  */ 9 class Solution {10 public:11     /**12      * @param head a ListNode13      * @param val an integer14      * @return a ListNode15      */16     ListNode *removeElements(ListNode *head, int val) {17         // Write your code here18         // Find the first non-Val node19         while ( head != NULL && head->val == val ) {20             head = head->next;21         }22         // If the head is NULL, return23         if ( head == NULL ) {24             return head;25         }26         // Remove the left val nodes27         ListNode *pre = head;28         ListNode *cur = pre->next;29         while ( cur != NULL ) {30             if ( cur->val != val ) {31                 pre->next = cur;32                 pre = pre->next;33             }34             cur = cur->next;35         }36         // In case of the tail has val node37         pre->next = cur;38         // return39         return head;40     }41 };

 

转载于:https://www.cnblogs.com/CheeseZH/p/4998696.html

转载地址:https://blog.csdn.net/weixin_30800807/article/details/94948126 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:TCP连接的建立和断开
下一篇:eCharts_基于eCharts开发的一个多图表页面

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月02日 14时33分50秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章