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 ...
随机推荐
- 【Leetcode】【Medium】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 超详细Redis入门教程【转】
这篇文章主要介绍了超强.超详细Redis入门教程,本文详细介绍了Redis数据库各个方面的知识,需要的朋友可以参考下 [本教程目录] 1.redis是什么 2.redis的作者何许人也 3.谁在使 ...
- JAVA读取HDFS信息
uri填路径 public static void main(String[] args) throws IOException { String uri = "/user/WeiboAD/ ...
- simotion连接 V90 1FL6 增量型电机,报警20025 编码器细分设置
V90 1FL6 增量型电机 The configured fine resolution for Gx_XIST1 (Encoder_N.absEncoder.absResolutionMultip ...
- CefSharp 浏览器核心,爬虫
CefSharp是什么 A framework for embedding web-browsing-like capabilities to a standard .NET application ...
- [原创] SiteServer 3.5 批量导入文章的SQL处理脚本
2005时做过一个小网站,当时是用ASP+Access做的,功能很简单,但里面的文章不少 现在就像把它转移到SS上来,重点就是如何导入文章 本来SS本身提供了批量导入功能,但对于在WEB上一次性导入一 ...
- JDBC(2)Statement
Statement: 用于执行SQL语句的对象 通过Connection的createStatement()方法得到一个Statement对象 只有在获得了Statement对象之后才能执行SQL对象 ...
- 可决系数R^2和MSE,MAE,SMSE
波士顿房价预测 首先这个问题非常好其实要完整的回答这个问题很有难度,我也没有找到一个完整叙述这个东西的资料,所以下面主要是结合我自己的理解和一些资料谈一下r^2,mean square error 和 ...
- POJ 1949 Chores (很难想到的dp)
传送门: http://poj.org/problem?id=1949 Chores Time Limit: 3000MS Memory Limit: 30000K Total Submissio ...
- 绘图驱动-OSD原理2
转载自:http://blog.pfan.cn/programming/21209.html 现在已经可以通过修改存储单元内容来改变OSD的像素,但还有个关键的问题是如何根据需要来进行操作,即如何将某 ...