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

Given linked list -- head = [4,5,1,9], which looks like following:

    4 -> 5 -> 1 -> 9

Example 1:

Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list
  should become 4 -> 1 -> 9 after calling your function.

Example 2:

Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You are given the third node with value 1, the linked list
should become 4 -> 5 -> 9 after calling your function.

Note:

  • The linked list will have at least two elements.
  • All of the nodes' values will be unique.
  • The given node will not be the tail and it will always be a valid node of the linked list.
  • Do not return anything from your function.

写一个函数删除单链表中的一个节点,链表至少有2个元素,所有的节点值是唯一的,给的节点不会是尾部并且是合法的节点,不返回任何值。要求in-place。

解法:先把next节点的值赋给当前节点,在把当前节点的next变成当前节点的next.next。

Java:

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

Python:

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
node.val = node.next.val
node.next = node.next.next 

Python:

class Solution:
# @param {ListNode} node
# @return {void} Do not return anything, modify node in-place instead.
def deleteNode(self, node):
if node and node.next:
node_to_delete = node.next
node.val = node_to_delete.val
node.next = node_to_delete.next
del node_to_delete  

C++:

void deleteNode(ListNode* node) {
*node = *node->next;
}  

C++:

class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val;
ListNode *tmp = node->next;
node->next = tmp->next;
delete tmp;
}
};

JavaScript:

var deleteNode = function(node) {
node.val = node.next.val;
node.next = node.next.next;
};

Ruby:  

def delete_node(node)
node.val = node.next.val
node.next = node.next.next
nil
end

  

类似题目:

[LeetCode] 203. Remove Linked List Elements 移除链表元素

 

 

All LeetCode Questions List 题目汇总

[LeetCode] 237. Delete Node in a Linked List 删除链表的节点的更多相关文章

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

  2. LeetCode #237. Delete Node in a Linked List 删除链表中的节点

    https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 非常巧妙的一道题. 题目没有给head,心想没有head我怎么才能找到要删 ...

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

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

  5. 237 Delete Node in a Linked List 删除链表的结点

    编写一个函数,在给定单链表一个结点(非尾结点)的情况下,删除该结点. 假设该链表为1 -> 2 -> 3 -> 4 并且给定你链表中第三个值为3的节点,在调用你的函数后,该链表应变为 ...

  6. [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 (在链表中删除一个点)

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

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

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

随机推荐

  1. Beta冲刺(1/7)——2019.5.22

    所属课程 软件工程1916|W(福州大学) 作业要求 Beta冲刺(1/7)--2019.5.22 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪万里 ...

  2. python练习题(四)

    题目: 根据一个字符串返回一个ip数组,按照ip最后一位排序, 字符串: str = 'ss192.0.0.12?!289.0.0.1!0.0.0.0!192.163.10.28?192.0.0.5' ...

  3. centos7部署postgresql集群高可用 patroni + etcd 之patroni篇

    实验环境:centos7.4纯净版 postgres版本: 9.6.15 etcd版本:3.3.11 patroni版本:1.6.0 patroni介绍可参考:https://github.com/z ...

  4. js数组操作 求最大值,最小值,正序、倒叙大小值排序,去重复

    var arr = [1,5,2,56,12,34,21,3,5] Math.min.apply({},arr) Math.max.apply({},arr) arr.sort((m,n)=>m ...

  5. python的信号管理

    if __name__ == '__main__': # Make it possible to exit application with ctrl+c on console signal.sign ...

  6. C#线程池 ThreadPool

    什么是线程池 大家都知道,我们在打开一个应用的时候,操作系统是要做很多的事情的,动态链接.装载.分配虚拟空间.等等等等,其实一个应用的打开同时也伴随着一个进程的建立. 进程的建立是需要时间的,在进程上 ...

  7. codeforces1276A As Simple as One and Two

    C.As Simple as One and Two A. As Simple as One and Two time limit per test 3 seconds memory limit pe ...

  8. springcloud - bus

    在重新设置了后的bootstrap.yml和application.yml后,可以看到bus-refresh的端点请求了.在之前bootstrap也可以设定哪个端点是可见,哪个未见. 如: #actu ...

  9. Android入门教程(一)

    Android是什么? Android是基于Linux开发性内核的操作系统,该平台由操作系统,中间件,用户界面和应用软件组成.Android的基本信息:软件类型,APK,发行商:Google,最早版本 ...

  10. hadoop jps 出现空指针错误

    在hadoop中安装jdk软件以后出现如下问题: 错误描述 [xxx@localhost jdk1..0_181]$ ./bin/jps Exception in thread "main& ...