题目

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. AD中添加中文字符丝印的方法:

    一 一般中文丝印: 用快捷键L打开层管理,在View options中勾选convert special 选项: 用快捷键P,S文本中输入你要的汉字,选中ture type,在select ture ...

  2. SpringMVC对HTTP报文体的处理

     客户端和服务端HTTP报文传递消息,而HTTP报文包含报文头和报文体.通常,解析请求参数以及返回页面都不需要我们关心HTTP报文体的读取和生成过程.但在某些特定场景下需要直接到请求报文中读取报文体, ...

  3. Oracle中文乱码,字符集问题处理

    1. 右键计算机,选择属性,增加环境变量 NLS_LANG:SIMPLIFIED CHINESE_CHINA.ZHS16GBK 2.进入注册表,依次单击HKEY_LOCAL_MACHINE --> ...

  4. 字符串和byte数组的相互转化

    关于byte[]数组转十六进制字符串: public static String getHexString(byte[] b) throws Exception { String result = & ...

  5. python3 socke 服务端与客户端实现(回炉)

    #服务端#!/usr/bin/env python3 # -*- coding:utf-8 -*- from socket import * # 创建socket tcpSerSocket = soc ...

  6. 【extjs6学习笔记】1.3 初始:根据模板创建项目

    使用sencha创建应用 命令说明:sencha -sdk /path/to/sdk generate app -s /your/templates/path/ MyApp /path/to/myap ...

  7. 1.2 the structure of a compiler

    Compiler 1.2 the structure  of a compiler Compiler : analysis and synthesis syntactically  语法上的 sema ...

  8. JAVA-Web05

    1 HTTP协议特点   1)客户端->服务端(请求request)有三部份 a)请求行 b)请求头 c)请求的内容,如果没有,就是空白字符     2)服务端->客户端(响应respon ...

  9. HDU 1124 Factorial (阶乘后缀0)

    题意: 给一个数n,返回其阶乘结果后缀有几个0. 思路: 首先将n个十进制数进行质因数分解,观察的得到只有2*5才会出现10.那么n!应含有min(2个数,5个数)个后缀0,明显5的个数必定比2少,所 ...

  10. SAP Cloud for Customer客户主数据的重复检查-Levenshtein算法

    SAP C4C的客户主数据创建时的重复检查,基于底层HANA数据库的模糊查找功能,根据扫描数据库中已有的数据检测出当前正在创建的客户主数据是否和数据库中记录有重复. 在系统里开启重复检查的配置: 在此 ...