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 --& ...
随机推荐
- GPU crash unmap page access
这类gpu crash是 texture 没有gpu address 调试方法 去看texture, texture state 里面allocateMemoryBlock...这里面有gpuadd ...
- C++时间标准库时间time和系统时间的使用
#include <iostream> #include <time.h> #include <stdio.h> #include <windows.h> ...
- eclipse下使用API操作HDFS
1)使用eclipse,在HDFS上创建新目录 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Fil ...
- Pytho中两种方式导入模块的差别
1.使用import module,只是把模块导入,访问模块中的函数名或者是属性是必须使用点运算符(.)来访问,否则直接访问会提示找不到这些函数或者属性. 2.使用from numpy import ...
- 用VBS将PPT转为图片
'使用方法:把ppt文件拖放到该文件上. '机器上要安装Powerpoint程序 On Error Resume Next Set ArgObj = WScript.Arguments pptfile ...
- ZOJ3238 Water Ring(计算几何)
题意:给你一个圆形和很多个矩形,然后要你求圆形的圆周有多少被矩形覆盖. 思路:比赛的时候是有思路的了,不过一直在调别的题,最后剩下30分钟肯定来不及敲.想法是这样的,要是我们可以求出每个矩形覆盖了圆周 ...
- POJ 2724
Purifying Machine Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4014 Accepted: 1127 ...
- Flatty Shadow在线为Icon图标生成长阴影效果。
Flatty Shadow在线为Icon图标生成长阴影效果. Flatty Shadow 彩蛋爆料直击现场 Flatty Shadow在线为Icon图标生成长阴影效果.
- POJ 1961
#include<iostream> #include<stdio.h> #define MAXN 1000001 using namespace std; char c[MA ...
- 自动化 测试框架部署(python3+selenium2)
安装Python 从https://www.python.org/downloads/下载最新版本的Python3,请注意,是3: 需要将Python的安装目录和安装目录下的Scripts文件夹添加到 ...