Leecode刷题之旅-C语言/python-203移除链表元素
/*
* @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移除链表元素的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
随机推荐
- Linux入门-2 VIM基础
启动与退出 模式 进入插入模式 命令 删除.复制.粘贴 光标控制 查找与替换 EX模式 启动与退出 vim只启动vim vim <filename>打开文件,如果不存在则新建 模式 Nor ...
- 在IIS7上导出所有应用程序池的方法 批量域名绑定
在IIS7+上导出所有应用程序池的方法:%windir%/system32/inetsrv/appcmd list apppool /config /xml > c:/apppools.xml ...
- yii2.0里别名的定义
别名用来表示文件路径和URL,为了避免在代码中硬编码一些绝对路径和URL,一个别名必须以‘@’符开头. 用Yii::setAlias()的方法来设置: //文件别名 Yii::setAlias('@f ...
- avl 平衡搜索二叉树的旋转图示
avl树的平衡是通过旋转不平衡子树完成的,旋转是如何完成的?这有幅不错的图http://upload.wikimedia.org/wikipedia/en/1/15/Tree_Rotations.gi ...
- PHP5.5的新特性
看了@轩脉刃 今天出炉的PHP 5.5 新特性.不过没有翻译全,我这里稍微补充下,整理成完整的一篇:) 原文:http://www.php.net/manual/zh/migration55.new- ...
- vector erase
vector::erase 从指定容器删除指定的元素 两个重载: iterator erase (iterator position);删除指定位置position的元素,并返回删除元素的下一个元素的 ...
- webstorm&phpstorm打开大型项目卡死解决如vue-laravel-Yii2
用phpstorm开发时如果项目中文件过多会造成phpstorm变慢甚至卡死,尤其在node加入到我们的项目中更加会加重这种情况,因为node_modules目录中的模块非常多,phpstorm加载这 ...
- UglifyJS 压缩选项
UglifyJS 压缩选项 1.使用逗号运算符连接简单语句 2.使用点符号代替中括号属性 foo [“bar”]→foo.bar 3.删除逻辑上走不到的代码 4.删除调试代码 debug ...
- 系统剪切板的使用UIPasteboard
最近发现支付宝和淘宝使用吱口令和淘口令的功能,就一直想怎么实现的,觉得应该是使用了系统的剪切板,然后查阅了资料做下笔记! 系统的剪切板主要是使用了UIPasteboard这个类. UIPasteboa ...
- Extjs header column 自定义排序规则
Extjs 的表格自带排序功能,这个功能在大部分情况下能够满足我们的需求,但是在某种情况下,例如IP排序,默认情况下,按照字符串进行排序, 此时我们需要自定义排序规则,这个时候就需要我们重写方法了, ...