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。

节点代码:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/

思路:容易想到,传入一个node,在只允许访问node 的情况下,可以知道的值有: node.val,  node.next,node.next.val,node.next.next.....(若非空)

假设有一个链表如图所示  :node 是节点2,它前面有节点1,后面有节点3。结合题意,如下步骤进行删除:

  1. 如果删除掉或者用节点3代替node(节点2),那么节点1将会丢失,因为并不能改掉1.next的指向,因此要保留node节点和它的前驱节点。
  2. 在保留节点node的情况下,只需把node.next节点(即节点3)的值去替换节点node的值,那么这个node将会变成节点3,即伪删除了(并没有真正从内存空间删除node,只是替换值而已)(蓝色线条)
  3. 再把最后一个节点(节点4)替换节点3,即node.next=node.next.next,真正从内存空间删除了node.next(即节点3被删除了,节点4替代了节点3)(棕色线条)
  4. 最后,得到一个删除了node的节点的链表(然而实际删除的是node.next,node只是值替换了而已)

                        

代码如下:

public class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}

237. Delete Node in a Linked List(leetcode)的更多相关文章

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

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

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

  5. [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 ...

  6. LeetCode 237. Delete Node in a Linked List 删除链表结点(只给定要删除的结点) C++/Java

    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

    题目: 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

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  9. Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]

    题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...

随机推荐

  1. Java程序设计——学生基本信息管理系统(团队+个人)

    学生信息管理系统(From:单身贵族) 团队部分 一.团队介绍("单身贵族"): 吴剑通[组长]:201521123056,唯一队员,网络1512班,团支书 二.项目git地址 三 ...

  2. HashMap、HashTable、ArrayList、LinkedList、Vector区别

    HashTable和HashMap区别 ①继承不同. public class Hashtable extends Dictionary implements Map public class Has ...

  3. 谈一谈最近学了一段时间的node.js

    官方说明 1.NodeJS宣称其目标是“旨在提供一种简单的构建可伸缩网络程序的方法”. 2.node是一个基于Chrome V8引擎进行代码解释的.轻量.可伸缩的具有事件驱动和非阻塞I/O机制的js运 ...

  4. PHP防SQL注入攻击

    PHP防SQL注入攻击 收藏 没有太多的过滤,主要是针对php和mysql的组合. 一般性的防注入,只要使用php的 addslashes 函数就可以了. 以下是一段copy来的代码: PHP代码 $ ...

  5. oc __weak和__strong的区别

    1.先上代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 id __weak obj=[[NSObject alloc]init];     NSLog(@"弱引 ...

  6. jsp base路径

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  7. 初学node.js有感三

    WebStorm下的node.js 一.回顾与继续       在前面,我们知道了node.js的基本框架和思路,在这些原生环境下我们对node.js的设计思想有了比较深刻的认识,并且具有了编写大型程 ...

  8. hdu1556树状数组的区间更新单点查询

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. R语言包下载(转载)

    http://blog.csdn.net/hongjinlongno1/article/details/53130893 包含几乎所有包,很方便

  10. 在CentOS6上利用PXE+Kickstart+Apache+DHCP实现无人值守安装

    在CentOS6上利用PXE+Kickstart+Apache+DHCP实现无人值守安装 1.PXEServer:OS:CentOS6.9IP:172.16.25.69: (1)apache:# mo ...