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.


题目标签:Linked List

  题目给了我们一个点,让我们在链表中删除这个点。

  一般来说,要删除一个点,首先想到的是 把这个点的前面一个点 直接 链接到 这个点的 后面一个点 就可以了。

  但是这一题只给了我们要删除的这个点(不会是最后一个点),所以回不到上一个点。

  只要改变一下想法,用下一个点的val 复制到这一个点就可以了,然后把这一个点 链接到 下下个点就可以了。

Java Solution:

Runtime beats 9.42%

完成日期:06/12/2017

关键词:singly-linked list

关键点:把下一个点的val 复制到这一个点

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution
{
public void deleteNode(ListNode node)
{
// copy next node val into this node, then link this node to next next node
ListNode nextNode = node.next;
node.val = nextNode.val;
node.next = nextNode.next; nextNode.next = null; // unlink nextNode to null }
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 237. 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] 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 ...

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

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

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

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

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

  8. LeetCode 237. Delete Node in a Linked List 删除链表结点(只给定要删除的结点) C++/Java

    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_Easy tag: Linked List

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

随机推荐

  1. (转)Hibernate框架基础——映射集合属性

    http://blog.csdn.net/yerenyuan_pku/article/details/52745486 集合映射 集合属性大致有两种: 单纯的集合属性,如像List.Set或数组等集合 ...

  2. python json格式和csv文件转换

    python json格式和csv文件转换 上代码 import csv import json ''' json格式示例 [{ "firstName":"Bill&qu ...

  3. Stuts2学习——HelloWorld

    这两天从对Struts一窍不通到成功运行HelloWorld,在SSH这条路上迈出了第一步. 下面我把我的第一个Struts程序放上来: 一.新建web project,配置文件等准备工作 1. 新建 ...

  4. CCF201509-2 日期计算 java(100分)

    试题编号: 201509-2 试题名称: 日期计算 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定一个年份y和一个整数d,问这一年的第d天是几月几日? 注意闰年的2月有2 ...

  5. eshing wind/tidal turbine using Turbogrid

    Table of Contents 1. meshing wind turbine using Turbogrid 1.1. ref 1 meshing wind turbine using Turb ...

  6. wake states

    Wake states Table of Contents 1. Turbulent Wake 2. turbulent wake of a circular cylinder 3. Referenc ...

  7. react----父子组件之间的参数传递

    1.父组件向子组件传递参数 //父组件 import React from 'react'; import './header.css' import ComponentChild from './h ...

  8. Cmake的介绍和使用 Cmake实践

    Cmake的介绍和使用 Cmake实践http://www.cppblog.com/Roger/archive/2011/11/17/160368.html

  9. hihocoder 1032 最长回文子串(Manacher)

    传送门 #include<queue> #include<cmath> #include<cstdio> #include<cstring> #incl ...

  10. Webstorm如何配置自动补全前缀--autoprefixer

    我们在写样式代码时,对不同平台会有不同的兼容性写法,会在代码前加前缀,但是手动加前缀很费时间而且很容易弄错.Webstorm编辑器是有自带补全前缀功能的,那为什么还要写这篇配置博客,因为Webstor ...