【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements

时间:2022-04-27 07:29:16

237 - Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

Hide Tags: Linked List

struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x), next(NULL){}
};
void deleteNode(ListNode *node)
{
/*the main problem is that we can not know the pre ListNode of node,
so we have to copy the val one by one*/
ListNode *cur=node, *post=cur->next;
while(post)
{
cur->val=post->val;
if(post->next==NULL)
cur->next=NULL;
else
cur=post;
post=cur->next;
}
}

203 - Remove Linked List 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

ListNode* removeElements(ListNode* head, int val)
{
ListNode *newhead=new ListNode(-);
newhead->next=head;
ListNode *pre=newhead;
ListNode *cur=head;
while(cur)
{
if(cur->val!=val)
pre=pre->next;
else
pre->next=cur->next;
cur=cur->next;
}
}

【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements的更多相关文章

  1. 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)

    [LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  2. 【leetcode】1019. Next Greater Node In Linked List

    题目如下: We are given a linked list with head as the first node.  Let's number the nodes in the list: n ...

  3. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

  4. 【LeetCode】237. Delete Node in a Linked List

    题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...

  5. LeetCode(237)Delete Node in a Linked List

    题目 Write a function to delete a node (except the tail) in a singly linked list, given only access to ...

  6. 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...

  7. 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...

  8. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  9. 【leetcode】955. Delete Columns to Make Sorted II

    题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...

随机推荐

  1. AC6102 开发板千兆以太网UDP传输实验

    AC6102 开发板千兆以太网UDP传输实验 在芯航线AC6102开发板上,设计了一路GMII接口的千兆以太网电路,通过该以太网电路,用户可以将FPGA采集或运算得到的数据传递给其他设备如PC或服务器 ...

  2. MSCRM 获取列表所选记录相关信息

    问题:如何通过JS获取列表中所选记录信息? 解决办法: The CRM2011 Ribbon has a special set of parameters called 'CrmParameters ...

  3. 网络编程之socket(转)

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价 值,那网络中进程之间如何通信,如我们每天打开浏 ...

  4. 关系数据库&&NoSQL数据库

    在过去,我们只需要学习和使用一种数据库技术,就能做几乎所有的数据库应用开发.因为成熟稳定的关系数据库产品并不是很多,而供你选择的免费版本就更加少了,所以互联网领域基本上都选择了免费的MySQL数据库. ...

  5. 寻找对象在父元素下的index

    方法一. window.onload=function(){    //寻找对象在父元素下的index    function getIndexParent(element){         var ...

  6. [Windows Phone] 如何撰写连接 Wifi、蓝芽、网路、飞航模式的网路设定功能

    原文:[Windows Phone] 如何撰写连接 Wifi.蓝芽.网路.飞航模式的网路设定功能 前言 为了可以使自己的 APP 具备操作网路的功能,在本文分享研究心得,包含在 Windows Pho ...

  7. linux signal

    1) SIGHUP 本信号在用户终端连接(正常或非正常)结束时发出, 通常是在终端的控制进程结束时, 通知同一session内的各个作业, 这时它们与控制终端不再关联. 登录Linux时,系统会分配给 ...

  8. 异常处理:try - except 和 try finally。

    异常处理:try-except语句 1)      此处:as reason为可选参数,reason是一个变量. 2)      使用try—except语句时,检测范围内出现错误,不会有红色的报错提 ...

  9. Win10+vs2012+cuda8.0的安装与配置

    安装环境说明:NVDIA GeForce 930M.Intel(R) HD Graphics 520 显卡和cuda需要兼容匹配,我一开始下载的cuda6.5无法安装,所以又重新下了比较新的cuda8 ...

  10. 在ajax post处理文件下载

    我有一个JavaScript应用程序需要使用ajax post请求发送到某个URL,然后后端会根据请求中的参数进行相应的工作,生成一个可下载的压缩包,等待下载.必须使用的ajax的原因是这里需要模拟提 ...