leetcode: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
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
分析:题意就是删除链表中指定的值。
一开始以为很简单,不就是链表中删除元素嘛,于是:
/**
* 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 *result = head;
while(head){
if(head->val==val){
if(head->next==NULL){
ListNode *temp = head;
head = head->next;
delete temp;
return head;
}
else{
ListNode* temp=head->next;
head->next=temp->next;
delete temp;
}
}
head=head->next;
}
return result;
}
};
显示出错:
/**
* 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 *result = head;
ListNode *p = head;
while (p && p->val == val)
{
ListNode *temp = p;
p = p->next;
delete temp;
}
result = p; //新的头指针 while (p)
{
if (p->next && p->next->val == val)
{
ListNode *temp = p->next;
p->next = p->next->next;
delete temp;
continue;
}
p = p->next;
} return result;
}
};
或者:可参考方案,1、采用两个指针p和q,p指向当前合法的元素,q指向下个可能合法的元素,合法的判断在于是否为空。
2、通过q指针遍历链表,如果当前元素合法,则p和q都向后移动,如果当前元素不合法,则p->next=q->next,跳过这个不合法的元素。注意只有在元素合法的时候才移动p。
#include<cstdio>
using namespace std; struct ListNode{
int val;
ListNode *next;
ListNode(int x): val(x), next(NULL){ }
};
ListNode* removeElements(ListNode *head, int val){
//p指向list中第一个合法的元素
ListNode *p = head;
while(p && p->val == val){
p = p->next;
}
if(p == NULL){
return NULL;
}
ListNode *q = p->next;
head = p; //新的head指针
//用q去遍历整个list
while(q != NULL){
if(q->val == val){
//q指向的元素要被删除
p->next = q->next;
q = q->next; //q继续向前移动
}
else { //找到了一个合法元素
p = p->next;
q = q->next;
}
}
return head;
}
int main(){
ListNode *p = new ListNode(1);
ListNode *q = new ListNode(2);
p->next = q;
ListNode *head = removeElements(p, 1);
printf("%d", head&&head->val);
return 1;
} </cstdio>
leetcode:Remove Linked List Elements的更多相关文章
- 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(52)-Remove Linked List Elements
题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 --> ...
- LeetCode OJ :Remove Linked List Elements (移除链表元素)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- leetcode解题报告(28):Remove Linked List Elements
描述 Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 ...
- LeetCode 203. Remove Linked List Elements (移除链表中的项)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- [LeetCode] 203. Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- 【leetcode】Remove Linked List Elements(easy)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- Java for LeetCode 203 Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- (easy)LeetCode 203.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
随机推荐
- Android系统Recovery工作原理
Android系统Recovery工作原理之使用update.zip升级过程分析(一)---update.zip包的制作 http://blog.csdn.net/mu0206mu/article/d ...
- Unity bundle的制作和使用
原地址:http://unity3d.9ria.com/?p=2863 Unity有个很好的功能,大致是很多专注于PC的engine没有提供的(因为没有必要),就是能加载主包外的资源,这个主包外的资源 ...
- Sqli-labs less 17
Less-17 本关我们可以看到是一个修改密码的过程,利用的是update语句,与在用select时是一样的,我们仅需要将原先的闭合,构造自己的payload. 尝试报错 Username:admin ...
- C#&Java重学笔记(集合比较和转换)
C#部分: 1.C#中集合有三种,数组类,ArrayList,和字典键值对类,一般也可以自定义集合,但是自定义集合的类型也只有这三类. 2.自定义集合实现三类集合的方法:前两者需要继承Collecti ...
- 【mysql5.6】下载安装
每次学新技术最烦的就是安软件了..... 下载的mysql5.6 http://dev.mysql.com/downloads/windows/ 一路默认安装.安装后 1.以管理员身份打开C:\WIN ...
- POJ 2141
#include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("1 ...
- jvm性能调优---jstat的用法
Jstat是JDK自带的一个轻量级小工具.全称“Java Virtual Machine statistics monitoring tool”,它位于java的bin目录下,主要利用JVM内建的指令 ...
- [你必须知道的.NET]第三十四回,object成员,不见了!
发布日期:2009.10.30 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 在.NET世界了,object是公认的造物主,其麾下的7大成员, ...
- C#中毫米与像素的换算方法
C#中以像素作为尺寸单位,像素是一种相对的尺寸概念,与毫米的转换与当前显示器的分辨率有关.在不同分辨率下转换的系数不同. 借助GDI可以完成毫米至像素的转换. public static double ...
- (KEIL)MDK5安装与JLINK问题解决方法(支持代码自动补全)
MDK V5在10月8日发布,昨天终于没忍住装上使用了一下,尝了尝鲜. 安装和破解的方法相信各位高手都不在话下,实在不会的可以参考keil4的安装步骤,keil5 和 keil4的安装没有的区别. ...