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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  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 to th ...

  5. (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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. 【BZOJ】【3171】【TJOI2013】循环格

    网络流/费用流 最后能走回出发点……说明全部是环= = 而二分图上的环说明什么呢……完备匹配 对于每个点,它都有四个可能的匹配点,且已知它已经(伪)匹配的一个点,那么我们于已知每条(伪)匹配边,我们连 ...

  2. 【BZOJ】【2631】Tree

    LCT 又一道名字叫做Tree的题目…… 看到删边加边什么的……又是动态树问题……果断再次搬出LCT. 这题比起上道[3282]tree的难点在于需要像线段树维护区间那样,进行树上路径的权值修改&am ...

  3. ios获取远程json数据

    NSMutableArray *arr; arr = [[NSMutableArray alloc]init];//全局 NSURL *url = [NSURL URLWithString:@&quo ...

  4. (转)Engineering Productivity

    (转)http://www.wandoujia.com/blog/from-qa-to-ep 这个文章之前读过,很不错.今天再读,有不一样的感受!推荐下. 下面是几段摘录: EP 是什么 说到这里,E ...

  5. 大漠推荐的教程:创建你自己的AngularJS -- 第一部分 Scopes

    创建你自己的AngularJS -- 第一部分 Scopes http://www.html-js.com/article/1863

  6. jboss 占用cpu 100%

    通过Java thread dump分析找到耗费CPU最高的源代码 分类: 9. Java2010-04-11 23:06 9272人阅读 评论(4) 收藏 举报 threadjavaeclipse插 ...

  7. 提权后获取linux root密码

    提权后获取linux root密码 2011-09-09 10:45:25     我来说两句      收藏    我要投稿 在webbackdoor本身是root(可能性小的可怜)或通过某漏洞溢出 ...

  8. H5时代的新存储简介

    1.WebStorage 分为:sessionStorage和localStorage两种,除了session的生命周期是在该域全部页面被关闭后就被清除而local是无限期存在外,二者的使用与方法属性 ...

  9. 初步体验javascript try catch机制

    javascript在ECMAScript3中引入了try catch finally机制,大致原理和其他语言一样. 我们也可以自定义错误事件. 但是事先声明:我们自定义的错误事件,只支持对name. ...

  10. spring_150804_controller

    实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...