leetcode解题报告(28):Remove Linked List Elements
描述
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
分析
先构造一个链表结点dummyHead,并让这个结点“插到”原链表头结点之前。举个例子:假设原链表是
1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6
通过dummyHead->next=head让原链表前多了这个结点,以方便操作:
INT_MIN --> 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6
再用一个指针cur指向这个新的链表的头结点,每次做如下操作:
- cur是否为空?若为空,退出循环,否则进入下一步;
- cur->next是否为空?若为空,说明已遍历完链表,令cur=cur->next,退出循环;否则,继续判断cur->next->val是否等于给定的值val,若不相等,则遍历下一元素,即令cur=cur->next。若相等,说明找到该元素,进行删除结点操作。
- 为了删除当前结点,需要用一个临时结点tmp指向该结点,然后让cur“跳过”该结点,跳过后,删除该结点,否则会造成内存泄漏。删除操作结束后,继续下一次循环。
代码如下:
/**
* 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(INT_MIN); //create a node as "new head"
dummyHead -> next = head;
ListNode* cur = dummyHead;
while(cur){
if(cur -> next && cur -> next -> val == val){ //be sure of that cur -> next is valid
ListNode* tmp = cur -> next; //store the node we want to delete
cur -> next = tmp -> next;
delete tmp; //delete the node or it may cause memory leak
}else
cur = cur -> next; //otherwise modify cur
}
return dummyHead -> next;
}
};
leetcode解题报告(28):Remove Linked List Elements的更多相关文章
- LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
- 【leetcode❤python】 203. Remove Linked List Elements
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements
237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- 【LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...
- LeetCode Remove Linked List Elements 删除链表元素
题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...
- 【刷题-LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...
- 203. Remove Linked List Elements - LeetCode
Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...
随机推荐
- Python之TensorFlow的卷积神经网络-5
一.卷积神经网络(Convolutional Neural Networks, CNN)是一类包含卷积计算且具有深度结构的前馈神经网络(Feedforward Neural Networks),是深度 ...
- 6:Partial Update 内部原理 和 乐观锁并发控制
Partial Update 内部执行过程: 首先,ES文档是不可变的,它们只能被修改,不能被替换.Update Api 也不例外. Update API 简单使用与之前描述相同的 检索-修改-重建索 ...
- Java调用WebService方法总结(1)--准备工作
WebService是一种跨编程语言.跨操作系统平台的远程调用技术,已存在很多年了,很多接口也都是通过WebService方式来发布的:本系列文章主要介绍Java调用WebService的各种方法,使 ...
- stage1----航空票务系统需求分析报告
航空票务管理系统需求分析报告 题 目 航空票务管理系统需求分析报告 学 院 信息科学与工程学院 专 业 计算机科学与技术 组 员 ...
- Part_four:redis主从复制
redis主从复制 1.redis主从同步 Redis集群中的数据库复制是通过主从同步来实现的 主节点(Master)把数据分发从节点(slave) 主从同步的好处在于高可用,Redis节点有冗余设计 ...
- SVN 提交失败 非LF行结束符
来源:http://programerni.diandian.com/post/2012-09-06/40037220960 我使用svn一直很顺利,今天在改了两个地方之后,提交时输入了两句话(只有两 ...
- Python多版本环境搭建(Linux系统)
python Linux 环境 (版本隔离工具) 首先新建用户,养成良好习惯 useradd python 1.安装pyenv GitHub官网: https://github.c ...
- Android笔记(五十九)Android总结:四大组件——Service篇
什么是服务? 服务(service)是Android中实现程序后台运行的解决方案,适用于去执行那些不需要和用户交互并且还需要长期运行的任务.服务的运行不依赖于任何用户界面. 服务运行在主线程中,所以在 ...
- Android笔记(五十六) Android四大组件之一——ContentProvider,实现自己的ContentProvider
有时候我们自己的程序也需要向外接提供数据,那么就需要我们自己实现ContentProvider. 自己实现ContentProvider的话需要新建一个类去继承ContentProvider,然后重写 ...
- linux下补丁制作和使用方法
两个文件的情况: 制作补丁: $ diff test1.c test2.c > test.patch 给test1.c打补丁: $ patch test1.c < test.patch 还 ...