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;
}
};

  显示出错:

      Input:[1,1], 1
     Output:[1]
      Expected:[]
   仔细审视代码发现自己确实疏漏了链表中连续两个都是制定删除元素的情况,而且这个逻辑是不正确的:每次删除操作时 ,都会执行head->next=head->next->next,而之后进行遍历下一个元素时head=head->next会跳过一个元素了,所以出错。
为了解决这个问题:
         
/**
* 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的更多相关文章

  1. 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题要求 ...

  2. LeetCode(52)-Remove Linked List Elements

    题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 --> ...

  3. LeetCode OJ :Remove Linked List Elements (移除链表元素)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  4. leetcode解题报告(28):Remove Linked List Elements

    描述 Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 ...

  5. LeetCode 203. Remove Linked List Elements (移除链表中的项)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  6. [LeetCode] 203. Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  7. 【leetcode】Remove Linked List Elements(easy)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  8. 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 -- ...

  9. (easy)LeetCode 203.Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

随机推荐

  1. Leetcode#126 Word Ladder II

    原题地址 既然是求最短路径,可以考虑动归或广搜.这道题对字典直接进行动归是不现实的,因为字典里的单词非常多.只能选择广搜了. 思路也非常直观,从start或end开始,不断加入所有可到达的单词,直到最 ...

  2. wifi current SSID

    1. 引入头,#import <SystemConfiguration/CaptiveNetwork.h> 2. 获取SSID info + (id)fetchSSIDInfo { NSA ...

  3. NGUI 自定义 Drag Item Script

    最近要实现一个NGUI效果. 查看了一下,NGUI有个自带 UIDragDropItem.cs 的组件进过修改后即可以实现. 下面贴上UI布局,代码: mDragDropItem.cs using U ...

  4. swfObject 使用说明

    1.Embed your SWF with JavaScript 使用方法  swfobject.embedSWF(swfUrl, id, width, height, version, expres ...

  5. 【回文字符串】 最长回文子串O(N) Manacher算法

    原理讲的清晰:Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串 注意: ①动态生命P[]和newStr数组后,不要忘记delete[] //其实这是基本的编码习惯 ②最终 ...

  6. 【五】PHP数组操作函数

    1.输出数组的结构:bool print_r(数组); $arr=array('jack','mike','tom'); print_r($arr);//Array ( [0] => jack ...

  7. SPL学习 迭代器

    主要学习内容: 慕课网的spl视频教程 阮一峰SPL学习笔记 http://www.ruanyifeng.com/blog/2008/07/php_spl_notes.html SPL类详解 http ...

  8. ubuntu下安装nodejs

    前言 继前几天在wins环境下使用cygwin模拟器安装nodejs出现了一些问题后,今天我决定在ubuntu下安装nodejs,安装过程非常顺利,没有报错,看来还是linux环境给力啊,由于刚接触l ...

  9. java基础知识回顾之javaIO类---InputStreamReader和OutputStreamWriter转化流

    InputStreamReader:是字节流通向字符流的桥梁: OutputStreamWriter 是字符流通向字节流的桥梁: package com.lp.ecjtu; import java.i ...

  10. android 四大组件Broadcast Receiver

    本文介绍Broadcast Receiver,包括几部分内容:Broadcast Receiver概述及实例.自定义Broadcast Receiver.Broadcast Receiver的实现细节 ...