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 ...
随机推荐
- StoreKit framework
关于In-APP Purchase 1. 中文文档 基本流程: http://www.cnblogs.com/wudan7/p/3621763.html 三种购买形式及StoreKit code介绍: ...
- STMPClient 发送邮件显示 不允许使用邮件名称.
在.net 2.0,3.5, 针对某些邮箱(还不清楚是什么样的邮件) , 使用微软自带的DLL发送邮件会提示不允许使用邮件名称 .... 使用Jmail可以发送. 解决方案: 1. ...
- Isomorphic JavaScript: The Future of Web Apps
Isomorphic JavaScript: The Future of Web Apps At Airbnb, we’ve learned a lot over the past few years ...
- ASP.NET MVC中几个运用技巧
1. Razor Helpers 的运用:例如,定义好 ViewBag.Message = "Welcome to ASP.NET MVC!";我要在界面上显示"Welc ...
- Ext Js学习之IIS理解
站点分为静态网站和动态网站,纯粹利用html编写的网站属于静态网站,不宜维护和更新而利用C#+extjs等前台+后台技术编写的网站就属于动态站点,有更多的交互,易维护和更新,比如降价的页面,利用htm ...
- 2016年度 JavaScript 展望(上)
[编者按]本文作者为资深 Web 开发者 TJ VanToll, TJ 专注于移动端 Web 应用及其性能,是<jQuery UI 实践> 一书的作者. 本文系 OneAPM 工程师编译呈 ...
- OPEN资讯
http://www.open-open.com/news/view/1f55540 随着 Android 平台市场份额的持续猛增 , 越来越多的开发者开始投入 Android 应用程序的开发大潮.如 ...
- ExtJs之 Ext.JSON
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- Xamarin for Visual Studio 3.11.666 稳定版 破解补丁 Version 3
前提概要 1.全新安装请参考 安装 Xamarin for Visual Studio. 2.本次补丁包含: ① Xamarin for Visual Studio 3.11.666 ② Xamari ...
- 2013 ACM/ICPC Asia Regional Online —— Warmup
1003 Rotation Lock Puzzle 找出每一圈中的最大值即可 代码如下: #include<iostream> #include<stdio.h> #inclu ...