LeetCode(203) Remove LinkedList 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.
分析
删除链表中的指定值的节点。
需要注意的是,所需要删除节点的位置,头,中间,尾,不同位置需要不同处理,避免断链~
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;
}
};
LeetCode(203) Remove LinkedList Elements的更多相关文章
- LeetCode(28)-Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode(27)Remove Element
题目 Given an array and a value, remove all instances of that value in place and return the new length ...
- LeetCode(26) Remove Duplicates from Sorted Array
题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode(82)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
- 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 ...
- LeetCode(83)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
随机推荐
- (转)通过MySQL复制线程SQL_Thread加快增量恢复binlog
数据回档常常是使用全量备份+binlog增量实现的.而数据量很大的情况下,增量恢复binlog一直是一个苦恼的问题,因为恢复binlog速度十分慢,并且容易出错. 恢复binlog文件一般有两种方法: ...
- Jenkins+Gitlab+Ansible自动化部署(二)
接Jenkins+Gitlab+Ansbile自动化部署(一):https://www.cnblogs.com/zd520pyx1314/p/10210727.html Ansible的配置与部署 工 ...
- HttpHelper使用记录
重新载入页面以获取源代码 var item = new HttpItem() { URL = @"http://www.xxx.com/msg/basic/?a=sendmsg", ...
- Tensorflow版Faster RCNN源码解析(TFFRCNN) (1) VGGnet_test.py
本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第1篇 VGGnet_test.py ----作者:Jiang Wu(吴疆),未经允许,禁止转载--- -- ...
- Spring Cloud 熔断器
目录 Spring Cloud 熔断器 Hystrix ribbon中使用hystrix feign中使用hystrix Spring Cloud 熔断器 在微服务架构中,根据业务来拆分成一个个的服务 ...
- The first step in solving any problem is recognizing there is one.
The first step in solving any problem is recognizing there is one.解决问题的第一步是要承认确实存在问题.
- 多线程串口通信 MFC CSerialPort
写在前面: 晚上应该继续完成未写完的代码,但Chrome上打开的标签实在太多了,约30个了,必须关掉一些,所以需要把自己看的整理一下然后关掉.本次主要写点MFC环境下多线程串口通信相关的东西,这包括线 ...
- gd调试命令,gdb调试core文件
使用 gcc -g test.c -o test.out 编译程序,只有加-g参数才支持gdb调试: 然后 gdb ./test.out 运行可执行文件,进入gdb调试模式(gdb),在括号后面的输入 ...
- SharePoint Online和SharePoint 2016 导出到Excel 表错误
导出到Excel是一个有用的SharePoint功能.偶尔您可能会遇到该功能无法正常工作的情况.有几个原因可能导致此功能无法正常工作. Problem #1 使用非32位Internet Explor ...
- SharePoint 2013 安装配置(3-1)
在第二部分中,我向您展示了如何在Windows Server 2012 R2 for SharePoint 2013上设置Active Directory域服务.现在我们应该能够在Active Dir ...