码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 203 移除链表元素

时间:2020-04-12 08:20:40      阅读:49      评论:0      收藏:0      [点我收藏+]

标签:删除   link   turn   move   while   val   tco   int   元素   

地址:https://leetcode-cn.com/problems/remove-linked-list-elements/
大意:删除链表中等于给定值的所有节点。

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if(head == NULL)
            return head;
        ListNode *hh = new ListNode(0);
        hh->next = head;
        ListNode *newNode = hh;
        while(newNode != NULL && newNode->next != NULL){
            if(newNode->next->val == val){
                newNode->next = newNode->next->next;
            }else{
                newNode = newNode->next;
            }
        }
        return hh->next;
    }
};

leetcode 203 移除链表元素

标签:删除   link   turn   move   while   val   tco   int   元素   

原文地址:https://www.cnblogs.com/c21w/p/12683523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!