Description:

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.

删除单向链表的一个节点,只给出了要删除的节点。

思路:从要删除的节点的下一个节点开始,逐一覆盖前面的节点的值,注意要删除最后一个多余的节点(Java中指向空即可,等待垃圾回收)。

代码:

/**
* 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 iNode = node;
while(iNode.next != null) {
//覆盖当前节点的值
iNode.val = iNode.next.val;
//删除最后一个多余的节点
if(iNode.next.next == null) {
iNode.next = null;
break;
}
iNode = iNode.next;
}
}
}

LeetCode——Delete Node in a Linked List的更多相关文章

  1. [LeetCode] 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 ...

  2. Leetcode 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 ...

  3. LeetCode Delete Node in a Linked List (删除链表中的元素)

    题意:给一个将要删除的位置的指针,要删除掉该元素.被删元素不会是链尾(不可能删得掉). 思路:将要找到前面的指针是不可能了,但是可以将后面的元素往前移1位,再删除最后一个元素. /** * Defin ...

  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 & 203 - Delete Node in a Linked List & Remove Linked List Elements

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

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

  7. [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

    2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...

  8. LeetCode_237. Delete Node in a Linked List

    237. Delete Node in a Linked List Easy Write a function to delete a node (except the tail) in a sing ...

  9. 【4_237】Delete Node in a Linked List

    Delete Node in a Linked List Total Accepted: 48121 Total Submissions: 109297 Difficulty: Easy Write ...

随机推荐

  1. jackson2.1.4 序列化 通过给定Class映射 与抽象类的映射

    //如果已知想要序列化的类型 可以使用TypeReference来进行处理 //List<MyBean> result = mapper.readValue(src, new TypeRe ...

  2. 一站式学习Wireshark(七):Statistics统计工具功能详解与应用

    Wireshark一个强大的功能在于它的统计工具.使用Wireshark的时候,我们有各种类型的工具可供选择,从简单的如显示终端节点和会话到复杂的如Flow和IO图表.本文将介绍基本网络统计工具.包括 ...

  3. HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is ...

  4. CentOS-6.4-minimal版中安装MongoDB-x86_64-3.0.2

    完整版见https://jadyer.github.io/2015/06/03/centos-install-mongodb/ /** * CentOS-6.4-minimal版中安装MongoDB- ...

  5. C#中对 XML节点进行添加,删除,查找和删除操作

    从网上整理所得 XMLDocument来操作XML比较简单,虽然有时效率不是很高.代码如下 已知有一个XML文件(bookstore.xml)如下: <?xml version="1. ...

  6. Go开发环境与LIteIDE安装、配置、搭建

    Go开发环境搭建 1.下载准备好安装包(Go-1.8.3.Git-2.9.2-64-bit) 下载链接:http://www.golangtc.com/download 2.配置环境变量 系统变量:新 ...

  7. webstorm软件使用记录

    右边的那条线的去除:setting-editor-appearance-show right margin 勾选去掉

  8. relation 关联模型

    关联关系必然有一个参照表,例如:有一个员工档案管理系统项目,这个项目要包括下面的一些数据表:基本信息表.员工档案表.部门表.项目组表.银行卡表(用来记录员工的银行卡资料).这些数据表之间存在一定的关联 ...

  9. C中入栈顺序和运算顺序有关系吗?

    如下代码会怎么执行? printf( "%c,%c,%c\n", getchar(), getchar(), getchar() ); 实际测试,是倒序执行,感觉上符合“C函数的形 ...

  10. linux -- 注销,关机,重启

      注销:logout Logout 注销是登陆的相对操作,登陆系统后,若要离开系统,用户只要直接下达logout命令即可: [root@localhost root]#logout Red Hat ...