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. JSP Workshop

    http://www.cnblogs.com/ITtangtang/p/4126395.html 发现http://www.tutorialspoint.com/里的资料很全也很不错啊! 资料:htt ...

  2. MVC 基础知识

    一. MVC架构1.MVC模式是一种严格实现应用程序各部分隔离的架构模式.隔离:分离关注点,松耦合2.模型(Model) 代表着核心的业务逻辑和数据.模型封装了域实体的属性和行为3.视图(View) ...

  3. ubuntu搭建lnmp

    http://wiki.ubuntu.org.cn/Nginx#.E5.AE.89.E8.A3.85Php.E5.92.8Cmysql

  4. Windows键盘快捷键

  5. 深入理解asp.net SessionState

    web Form 网页是基于HTTP的,它们没有状态, 这意味着它们不知道所有的请求是否来自同一台客户端计算机,网页是受到了破坏,以及是否得到了刷新,这样就可能造成信息的丢失. 于是, 状态管理就成了 ...

  6. laravel where中多条件查询

    1. http://www.mobanstore.com/doc/bianchengkaifa/119.html //初学laravel 发现他的查询构造器很好用 //如下 $user = DB::t ...

  7. ubuntu安装hive

    1.安装mysql,可参考下面链接 http://www.cnblogs.com/liuchangchun/p/4099003.html 2.安装hive,之前,先在mysql上创建一个hive,数据 ...

  8. Android Source Code

    一. Android 框架 http://elinux.org/Master-android Android框架层级 : Android 自下 而 上 分为 4层; -- Linux内核层; -- 各 ...

  9. sql连接字符串的方法

    ----乌龟代码---合并列值 --********************************************************************************** ...

  10. JDBC 程序的常见错误及调试方法

    详细介绍:http://dev.mysql.com/doc/refman/5.5/en/error-handling.html http://dev.mysql.com/doc/refman/5.5/ ...