/*
* @lc app=leetcode.cn id=203 lang=c
*
* [203] 移除链表元素
*
* https://leetcode-cn.com/problems/remove-linked-list-elements/description/
*
* algorithms
* Easy (39.58%)
* Total Accepted: 18.3K
* Total Submissions: 46.2K
* Testcase Example: '[1,2,6,3,4,5,6]\n6'
*
* 删除链表中等于给定值 val 的所有节点。
*
* 示例:
* p q
* 输入: 1->2->6->3->4->5->6, val = 6
* 输出: 1->2->3->4->5
*
*
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val) {
struct ListNode *p;
if(head==NULL){
return ;
}
while(head!=NULL&&head->val==val)
head = head->next; if(head==NULL)
return ; p = head;
while(p->next!=NULL){
if(p->next->val==val)
p->next=p->next->next;
else
{
p = p->next;
}
}
return head;
}

思路非常简单,相同的删去断链就行。然后开头检查直到head不等于val。

------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=203 lang=python3
#
# [203] 移除链表元素
#
# https://leetcode-cn.com/problems/remove-linked-list-elements/description/
#
# algorithms
# Easy (39.58%)
# Total Accepted: 18.3K
# Total Submissions: 46.2K
# Testcase Example: '[1,2,6,3,4,5,6]\n6'
#
# 删除链表中等于给定值 val 的所有节点。
#
# 示例:
#
# 输入: 1->2->6->3->4->5->6, val = 6
# 输出: 1->2->3->4->5
#
#
#
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
while head is not None and head.val == val:
head = head.next
current = head
while current is not None:
if current.next is not None and current.next.val == val:
current.next = current.next.next
else:
current = current.next
return head

Leecode刷题之旅-C语言/python-203移除链表元素的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

随机推荐

  1. linux定时备份MySQL数据库并删除七天前的备份文件

    1.创建备份文件夹 #cd /bak#mkdir mysqldata 2.编写运行脚本 #nano -w /usr/sbin/bakmysql.sh 注:如使用nano编辑此代码需在每行尾添加’&am ...

  2. 判断ORACLE启动时使用spfile还是pfile

    自Oracle 9i以后启动的时候默认使用的初始化文件是spfile,我们可以通过如下三种方式来判断是SPFILE还是PFILE方式启动数据库.1.show parameter spfile2.sho ...

  3. July 25th 2017 Week 30th Tuesday

    Everything is always more beautiful reflected in your eyes. 一切事物映在你的眼里都会变得更美. Looking in your eyes, ...

  4. OC基础数据类型-NSArray

    1.数组的初始化 NSArray *array = [[NSArray alloc] initWithObjects:@"One", @"Two", @&quo ...

  5. OC 指向指针的指针

    #import <Foundation/Foundation.h> void changeC(char *d) { *d = ; } void changeStr(NSString **s ...

  6. 获取Spring管理的Bean

    1.再Spring配置文件中配置工具类 <!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spr ...

  7. JQuery获取和设置Select选项的常用方法总结

    1.获取select 选中的 text:  $("#cusChildTypeId").find("option:selected").text();  $(&q ...

  8. 上传组件uploadify在spring中返回406 / Not Acceptable 问题解决

    这个问题在chrome中正常.在火狐和ie中就会报这个错误. 原因就是chrome的accept是*/* 火狐和ie的accept是text/* 但是spring的accept清单中是没有text/* ...

  9. ARM Linux 大小核切换 ——cortex-A7 big.LITTLE 大小核 切换代码分析

    ARM Linux 大小核切换——cortex-A7 big.LITTLE 大小切换代码分析 8核CPU或者是更多核的处理器,这些CPU有可能不完全对称.有的是4个A15和4个A7,或者是4个A57和 ...

  10. VS 2017 + EF6 + MySQL5.7 建立实体模型闪退问题

    具体环境是:VS2017 ..NET Framework 4.6 .MySql.Data.Entity 6.9.12 在这个环境下总是不成功,具体是在这一步闪退,也不报错: 在点击“下一步”后,没有进 ...