leetcode: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.
分析:题意即让我们删除链表的一个节点,但是却与我们熟知的一般情况不同,它没有给我们链表的起点,只给了我们一个要删的节点。
不太一样在于:我们之前要删除一个节点的方法是要有其前一个节点的位置,然后将其前一个节点的next连向要删节点的下一个,然后delete掉要删的节点即可。这道题的处理方法是先把当前节点的值用下一个节点的值覆盖了,然后我们删除下一个节点即可,代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
ListNode* temp=node->next;
node->val=node->next->val;
node->next=node->next->next;(也可写为node->next=temp->next;)
delete temp; }
};
或:
class Solution {
public:
void deleteNode(ListNode* node) {
ListNode* nextNode = node->next;
*node = *nextNode;
delete nextNode;
}
};
leetcode:Delete Node in a Linked List的更多相关文章
- [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 th ...
- LeetCode OJ: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 th ...
- 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 th ...
- [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 th ...
- (easy)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 th ...
- Java [Leetcode 273]Delete Node in a Linked List
题目描述: Write a function to delete a node (except the tail) in a singly linked list, given only access ...
- leetcode 237 Delete Node in a Linked List python
题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...
- 17.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 th ...
- [Leetcode]237. Delete Node in a Linked List -David_Lin
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
随机推荐
- [百度空间] [原]MFC杂乱笔记
1. 创建动态菜单 假如ID是动态分配的,那么重载virtual BOOLOnCmdMsg(UINT,int,void*,AFX_CMDHANDLERINFO*); 据MSDN不详细解释,当第二个参数 ...
- 如何创建和发布.asmx Web Service
创建和发布Web ServiceWeb服务方法中可以返回一个DataSet对象 WEB服务可以说是下一代WEB应用程序的基础,无论客户端是WINDOWS应用.ASP.NET Web Form程序.甚至 ...
- Codis集群的搭建与使用
一.简介 Codis是一个分布式的Redis解决方案,对于上层的应用来说,连接Codis Proxy和连接原生的Redis Server没有明显的区别(不支持的命令列表),上层应用可以像使用单机的Re ...
- Javascript 正则表达式笔记
\d 元字符 + 量词 \w 常用的字符a-zA-Z0-9 .除回车之外的字符 ?0-1个字符 量词 只有前面是元字符,才变现量词 * 0-n 量词 /^\d+$/ 以字符开头,义字符结尾 [0-9] ...
- ***php(codeigniter)中如何重定向
Q: 在保存完数据之后需要重定向,防止数据重复提交. 我使用$this->方法名();跳转,发现不能达到重定向的效果(地址栏没变) 请教高手重定向怎么用 A: $this->load-&g ...
- Android Grapics图像类体系
- Cpp多重继承会产生的问题
多重继承常常被认为是 OOP 中一种复杂且不必要的部分.多重继承面临 crash 的场景并非难以想象,来看下面的例子. 1. 名称冲突 来看以下情况: 如果 Dog 类以及 Bird 类都有一个名为 ...
- linux下对普通用户设置文件访问控命令之setfacl
命令名 setfacl -设置文件访问控制列表 常用用法:setfacl [-bkRd] [{-m|-x} acl参数] 目标文件名 命令的常用参数 -m 设置后续的acl参数给文件使用(常用). ...
- JSON 与JAVA对象之间的转换(转)
JSON与XML两者目前在数据交互方面都有很高的使用率,随着现在SOA的兴起,异构系统的整合地位相应提高,本篇文章主要介绍JSON与JAVA对象之间的相互转换. 一.对普通类型的JSON模式的转换 一 ...
- java三种调用方式(同步调用/回调/异步调用)
1:同步调用:一种阻塞式调用,调用方要等待对方执行完毕才返回,它是一种单向调用 2:回调:一种双向调用模式,也就是说,被调用方在接口被调用时也会调用对方的接口: 3:异步调用:一种类似消息或事件的机制 ...