Leetcode Delete Node in a Linked List
2016-07-07 21:11
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
the linked list should become
Difficulty: Easy
/**
* 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) {
ListNode pre = node;
node = node.next;
while(node.next != null){
pre.val = node.val;
pre = pre.next;
node = node.next;
}
pre.val = node.val;
pre.next = null;
return;
}
}
Supposed the linked list is
1 -> 2 -> 3 -> 4and you are given the third node with value
3,
the linked list should become
1 -> 2 -> 4after calling your function.
Difficulty: Easy
/**
* 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) {
ListNode pre = node;
node = node.next;
while(node.next != null){
pre.val = node.val;
pre = pre.next;
node = node.next;
}
pre.val = node.val;
pre.next = null;
return;
}
}
相关文章推荐
- 在github上搭建blog(基于Node.js的Hexo+next)
- Node.js:express (post get querystring json sendFile)
- 原生JS 循环Nodelist Dom列表的4种方式
- nodejs 设计思想杂记四 异步控制流模式
- Nodejs学习路线图
- Nodejs中关于类的创建
- LeetCode 19. Remove Nth Node From End of List详解
- 关于nodejs中使用命令行启动报错的问题
- 5. node.js 的 模块(Module)和包(Package)
- 3. node.js 异步式I/O或非阻塞式I/O
- 2.node.js 入门
- Reverse Nodes in k-Group
- 1.Node.js是什么?
- 《leetCode》: Count Complete Tree Nodes
- nodejs加密Crypto的实例代码
- Node.js DES加密的简单实现
- 推荐几个学习Node.js的站点
- Node.js 学习笔记
- Node.js des加密
- Restify一