题目:

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.

翻译:

写一个方法去删除单链表的一个节点(非尾节点),仅仅给出訪问这个节点的參数。

假定链表是1 -> 2 -> 3 -> 4,给你的节点是第三个节点(值为3),这个链表在调用你的方法后应该变为 1 -> 2 -> 4。

分析:

删除链表的节点的常规做法是改动上一个节点的next指针。然而这边并没有提供上一个节点的訪问方式。故转变思路改为删除下一节点。直接将下一个节点的val和next赋值给当前节点。

代码:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void deleteNode(ListNode node) {
node.val=node.next.val;
node.next=node.next.next;
}
}

Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]的更多相关文章

  1. 237. Delete Node in a Linked List【easy】

    237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...

  2. 237. Delete Node in a Linked List(C++)

    237. Delete Node in a Linked Lis t Write a function to delete a node (except the tail) in a singly l ...

  3. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

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

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

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

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

随机推荐

  1. Linux调用fork()编程

    本文出自:svitter's blog #include <iostream> #include <cstdio> #include <unistd.h> usin ...

  2. 当时用vuex的时候,使用...对象展开扩展符报错的解决办法

    出现这种问题的主要原因是当前的babel不支持...对象展开扩展符,只需要安装一个插件然后再在.babelrc当中进行下配置就好了 npm i babel-plugin-transform-objec ...

  3. MYSQL常用的Show命令笔记

    1.查询数据库 SHOW DATABASES; 2.查询数据库中表 SHOW TABLES; SHOW TABLES FROM db; 3.查询表索引 SHOW INDEX FROM table 4. ...

  4. 《Java性能权威指南》笔记----Java性能调优工具

    OS 1.CPU 用户态时间(us):cpu执行应用代码所占时间的百分比. 内核态时间(sy):cpu执行内核代码所占时间的百分比,系统态时间与应用相关. 空闲时间(id):cpu空闲时间百分比.空闲 ...

  5. POJ1927 Area in Triangle

      Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1458   Accepted: 759 Description Give ...

  6. flash编程实例源代码下载

    原文发布时间为:2008-08-20 -- 来源于本人的百度文章 [由搬家工具导入] http://library.sx.zj.cn/shgp/ActionScript编程实例详解.rar

  7. 原生app与js交互 jsSDK设计

    var UA = window.navigator.userAgent.toLowerCase()var isIOS = UA && /iphone|ipad|ipod|ios/.te ...

  8. poj 2280 Islands and Bridges 哈密尔顿路 状压dp

    题目链接 题意 给定一个\(N\)个点的无向图,求一条哈密尔顿路径\(C_1C_2...C_n\),使其\(value\)最大. \(value\)的计算方式如下:\[\begin{aligned}v ...

  9. VIM使用技巧3

    假如有如下代码: var  foo = "method("+argument1+","+argument2+")" 任务:在每个“+”前后各 ...

  10. C语言中的基础知识变量探讨

    C语言中的变量是编程的基础,主要有四个要素:存储类型.存储大小.存储名称和存储地址. 一.变量的要素: 1.存储类型:主要表明名变量存储的特征,主要有auto.extern.static和regist ...