题目:

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. iOS 代理为啥要用weak修饰?

    在开发中我们经常使用代理,或自己写个代理,而代理属性都用weak(assign)修饰,看过有些开发者用strong(retain),但并没发现有何不妥,也不清楚weak(assign)与strong( ...

  2. 兼容FF和IE的tooltip 鼠标提示框

    原文发布时间为:2009-09-07 -- 来源于本人的百度文章 [由搬家工具导入] http://www.walterzorn.de/tooltip/tooltip.htm 【请见该页面】 Down ...

  3. android开发过程遇到的一些错误

    Unable to resolve target "android-x" 这是工程的Android版本和本地SDK中的版本不一致,一般做下处理: 1. 右击项目->andro ...

  4. Qualcomm defconfig

    xxx_defconfig - for debugging xxx-perf_defconfig - for final live user's version.

  5. reportlab包使用指南

    reportlab.canvas有这六个主要参数 1.pagesize:设置纸张大小    #from reportlab.lib.pagesizes import letter, A4  导入常见的 ...

  6. hadoop(三)HDFS 文件系统

    Hadoop 附带了一个名为 HDFS(Hadoop 分布式文件系统)的分布式文件系统,专门 存储超大数据文件,为整个 Hadoop 生态圈提供了基础的存储服务. 本章内容: 1) HDFS 文件系统 ...

  7. LeetCode OJ-- Populating Next Right Pointers in Each Node II **@

    https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 接上一题目,输入的树不是perfect ...

  8. JS-JavaScript String 对象-string对象方法2: indexOf()、lastIndexOf()、charAt()

    1.indexOf():可返回某个指定的字符串值在字符串中首次出现的位置. 1).语法:string.indexOf(searchvalue,start):     searchvalue:必需.规定 ...

  9. dubbo框架的简单介绍

    以下的官网的介绍. dubbo是SOA.小例子是简单的远程调用(生产者消费者的模式出现).http://blog.csdn.net/huangyekan/article/details/4217267 ...

  10. ASIHTTPRequest 问题总结

    1, ASIHttpRequest与30秒超时 今天在项目中发现一个ASIHttpRequest的Bug.这个Bug可能会导致你Http请求延时至少在timeout设置时间结束之后.更可怕的是,为了找 ...