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.

因为没有办法去得到前面一个node的next, 所以我们可以将下一个node copy给当前的node值, 然后把node.next去掉即可. 机智.

Code

# 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

[LeetCode] 237. Delete Node in a Linked List_Easy tag: Linked List的更多相关文章

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

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

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

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

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

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

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

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

随机推荐

  1. 原生js--类的扩充和类型检测

    扩充类的方法: 1.向原型对象上添加方法或属性.例如:Number.prototype.cl = function(){}; 但这种做法并不推荐,因为ES5之前,无法将这些新添加的方法或属性设置为不可 ...

  2. 笔者使用macOS的一些经验点滴记录1

    (1) 输入法快捷键 ctrl+shift+p  拼音 ctrl+shift+W  五笔型 按CapsLock可以在英文与指定中文输入法间进行切换 (2) 定时关机 sudo shutdown -h ...

  3. linux的shell后门尝试以及Cython转成C代码编译

    零.背景 最近研究了一下之前的反弹shell的python代码块,写了一点代码尝试在LInux下绑定和反弹shell(正反向),看了一些代码,基本是两种思路.1.本地shell的输入输出通过管道与so ...

  4. 替换linux系统文件etc下passwd文件的字段获取真正的root权限

    sudo与root 很多人都知道使用linux可以利用sudo来执行一些root权限执行的事情,但是sudo和root还是有很大的区别的. 区别一:sudo可以执行的命令是root账户利用/etc/s ...

  5. STM8S 低功耗(1)

    STM8S的低功耗模式有4种. 关系如下. 本次使用的停机(HALT) 使用了内部定时器,外部中断,LED指示是否进入低功耗. 在while循环中设置如下 ) { if(!IR_IN) // 外部中断 ...

  6. VC++ 多线程编程,win32,MFC 例子(转)

    一.问题的提出 编写一个耗时的单线程程序: 新建一个基于对话框的应用程序SingleThread,在主对话框IDD_SINGLETHREAD_DIALOG添加一个按钮,ID为IDC_SLEEP_SIX ...

  7. iOS - 处理计算精度要求很高的数据,floatValue,doubleValue等计算不精确问题

    .问题描述:服务器返回的double类型9...94的数字时 .之前处理方式是 :(从内存.cpu计算来说double都是比较合适的,一般情况下都用double) goodsPrice.floatVa ...

  8. POJ-1458 Common Subsequence(线性动规,最长公共子序列问题)

    Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44464 Accepted: 18186 ...

  9. SequenceFile实例操作

    HDFS API提供了一种二进制文件支持,直接将<key,value>对序列化到文件中,该文件格式是不能直接查看的,可以通过hadoop  dfs -text命令查看,后面跟上Sequen ...

  10. codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]

    就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...