题目:

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. ip地址正则表达式

    p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$') if p.match(dom ...

  2. 【CF1043A】Elections(签到)

    题意:给定n个数字,第i个为a[i],求使得sigma k-a[i]>sigma a[i]最小的k n,a[i]<=1e2 思路: #include<cstdio> #incl ...

  3. [转]从头到尾彻底理解KMP

    https://blog.csdn.net/v_july_v/article/details/7041827

  4. 让Dropdownlist既有静态项又有动态项或者既能有编辑项又能绑定数据源

    原文发布时间为:2008-10-27 -- 来源于本人的百度文章 [由搬家工具导入] protected void Page_Load(object sender, EventArgs e) //Dr ...

  5. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---24

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  6. C++ primer分章节快速回顾

    第三章: 1,sozeof(int): int n_int=INT_MAX; sizeof n_int;(对变量括号可选) 2,#include<climits>包含一些类型的最大值3,c ...

  7. 大型网站优化-memcache技术

    大型网站优化-memcache技术 memory+cache 内存缓存 memcache简介 memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发 ...

  8. 根据CPU核数合理设置线程池大小

    一般来说池中总线程数是核心池线程数量两倍,只要确保当核心池有线程停止时,核心池外能有线程进入核心池即可. 我们所需要关心的主要是核心池线程的数量该如何设置. 自定义线程池代码 package com. ...

  9. Ruby on rails初体验(二)

    体验一中添加了一个最基本的支架和一个简单的数据迁移,实现了一个基本的增删改查的功能列表.体验二中要在次功能上继续丰满一下功能.实现如下效果: 在每个公司中都包含有不同的部门,按照体验一中的方法,添加一 ...

  10. WPF 自动验证

    WPF中TextBox的自动验证: 演示 : 用以下两个TextBox分别显示验证IP和非空值验证,先看效果: IP自动验证效果: 非空值自动验证效果: 第一步:定义TextBox验证的样式: < ...