题目

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.

分析

删除链表中的指定值的节点。

需要注意的是,所需要删除节点的位置,头,中间,尾,不同位置需要不同处理,避免断链~

AC代码

/**
* 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 head; ListNode *pre = head, *p = head;
while (p)
{
//找到要删除的节点元素
if (p->val == val)
{
//判断当前节点为头结点
if (p == head)
{
head = p->next;
ListNode *q = p;
//更新头
p = head;
pre = head;
delete q;
q = NULL;
}
//删除尾节点
else if (p->next == NULL)
{
pre->next = NULL;
delete p;
p = NULL;
}
//删除链表中间节点
else{
pre->next = p->next; ListNode *q = p;
p = p->next;
delete q;
q = NULL;
}//else
}//if
else{
pre = p;
p = p->next;
} }//while
return head;
}
};

GitHub测试程序源码

LeetCode(203) Remove LinkedList Elements的更多相关文章

  1. LeetCode(28)-Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  2. LeetCode(80)Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode(27)Remove Element

    题目 Given an array and a value, remove all instances of that value in place and return the new length ...

  4. LeetCode(26) Remove Duplicates from Sorted Array

    题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  5. LeetCode(82)Remove Duplicates from Sorted List

    题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...

  6. LeetCode(19) Remove Nth Node From End of List

    题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...

  7. LeetCode(83)Remove Duplicates from Sorted List

    题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...

  8. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  9. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

随机推荐

  1. (转)通过MySQL复制线程SQL_Thread加快增量恢复binlog

    数据回档常常是使用全量备份+binlog增量实现的.而数据量很大的情况下,增量恢复binlog一直是一个苦恼的问题,因为恢复binlog速度十分慢,并且容易出错. 恢复binlog文件一般有两种方法: ...

  2. Jenkins+Gitlab+Ansible自动化部署(二)

    接Jenkins+Gitlab+Ansbile自动化部署(一):https://www.cnblogs.com/zd520pyx1314/p/10210727.html Ansible的配置与部署 工 ...

  3. HttpHelper使用记录

    重新载入页面以获取源代码 var item = new HttpItem() { URL = @"http://www.xxx.com/msg/basic/?a=sendmsg", ...

  4. Tensorflow版Faster RCNN源码解析(TFFRCNN) (1) VGGnet_test.py

    本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第1篇   VGGnet_test.py ----作者:Jiang Wu(吴疆),未经允许,禁止转载--- -- ...

  5. Spring Cloud 熔断器

    目录 Spring Cloud 熔断器 Hystrix ribbon中使用hystrix feign中使用hystrix Spring Cloud 熔断器 在微服务架构中,根据业务来拆分成一个个的服务 ...

  6. The first step in solving any problem is recognizing there is one.

    The first step in solving any problem is recognizing there is one.解决问题的第一步是要承认确实存在问题.

  7. 多线程串口通信 MFC CSerialPort

    写在前面: 晚上应该继续完成未写完的代码,但Chrome上打开的标签实在太多了,约30个了,必须关掉一些,所以需要把自己看的整理一下然后关掉.本次主要写点MFC环境下多线程串口通信相关的东西,这包括线 ...

  8. gd调试命令,gdb调试core文件

    使用 gcc -g test.c -o test.out 编译程序,只有加-g参数才支持gdb调试: 然后 gdb ./test.out 运行可执行文件,进入gdb调试模式(gdb),在括号后面的输入 ...

  9. SharePoint Online和SharePoint 2016 导出到Excel 表错误

    导出到Excel是一个有用的SharePoint功能.偶尔您可能会遇到该功能无法正常工作的情况.有几个原因可能导致此功能无法正常工作. Problem #1 使用非32位Internet Explor ...

  10. SharePoint 2013 安装配置(3-1)

    在第二部分中,我向您展示了如何在Windows Server 2012 R2 for SharePoint 2013上设置Active Directory域服务.现在我们应该能够在Active Dir ...