Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]
2016-03-02 11:36
351 查看
题目:
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赋值给当前节点。
代码:
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赋值给当前节点。
代码:
[code]/** * 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; } }
相关文章推荐
- HDFS中的NameNode 和 DataNode
- forever让nodejs应用后台执行
- nodejs-helloword案例
- nodejs安装
- node.js-概念
- Leetcode Swap Nodes in Pairs
- node和ionic组合下载真难寻找
- nodejs express
- LinkedList---Delete Node in the Middle of Singly Linked List
- Win7环境下Sublime Text 3下安装NodeJS插件
- nodejs String
- nodejsPath
- ubuntu如何安装nodejs最新版 本
- nodejs 自动重启服务 supervisor
- 通过inode删除指定文件
- 在Windows上安装Node.js模块的方法
- ## 创建第一个node.js工程 ##
- cesium使用NODEJS搭建环境
- win系统下node.js安装及环境配置.
- nodejs 小技巧——使用 supervisor