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:

Example 1:

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

Example 2:

Input: head = [,,,], node =
Output: [,,]
Explanation: You are given the third node with value , the linked list should become -> -> 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.

解题思路:与之前删除链表结点不同的是:这道题只给了我们要删除的那个结点,并没有给出链表的头结点,所以无法找到上一个结点,顺链删除此结点。我们可以将下一个结点的值覆盖在当前节点上,删除下一个节点即可。

C++:

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

Java:

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

LeetCode 237. Delete Node in a Linked List 删除链表结点(只给定要删除的结点) C++/Java的更多相关文章

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

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

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

  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_Easy tag: 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 解题报告

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

随机推荐

  1. Euclid's Game

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s ...

  2. Favorite Donut(HDU 5442)最小表示法+二分

    题目给出一个字符串,由a~z表示甜度,随字典序增大,字符串首尾相连形成一个圈,要求从一个位置开始字典序最大的字符串,输出位置以及是顺时针还是逆时针表示.顺时针用0表示,逆时针用1表示. 此题只需要查找 ...

  3. Django-api认证

    验证过程:每一个中控机有一个key,然后获取到当前时间戳.把key和当前时间戳这两个值进行一次加密,加密之后得到加密的戳,把加密的戳和当前获取的时间戳用管道符分割开生成一个大的字符串(加密戳|时间戳) ...

  4. VS2017 C/C++输入密码显示*星号

    VS2017  C/C++输入密码显示*星号 _getch()函数使用时遇到的坑 参考: https://blog.csdn.net/guin_guo/article/details/46237905 ...

  5. 联想拯救者r720+固态浦科特M8PeGN 的bios 设置

    最近笔记本左边的两个usb3.0接口都坏了,win10 报错usb端口上的电涌 真是一脸蒙B! 然后去打售后电话,说昌平没有售后,需要去海淀黄庄去维修,然后下午就去了那里,维修的人员说硬件坏了,需要1 ...

  6. Ubuntu中,wxpython的TextCtrl引发的error:_pixman_log_error

    1>在windows下,执行下面的代码,不会报错,但是在Ubuntu下,就会报错 2>在Ubuntu下执行上面的代码,会提示下面的错. 解决方法: 若是TextCtrl中不写wx.TE_M ...

  7. win10刻录光盘失败,一直显示有准备好写入到光盘中的文件

    这是因为前面刻录留下的缓存导致的 解决方法是,删除系统刻录缓存文件 刻录缓存路径大概在以下位置(其中ase那个地方需要修改,改成自己的登录用户账号名即可) C:\Users\ase\AppData\L ...

  8. IDEA 代码生成插件 CodeMaker

    Java 开发过程中经常会遇到编写重复代码的事情,例如说:编写领域类和持久类的时候,大部分时候它们的变量名称,类型是一样的,在编写领域类的时候常常要重复写类似的代码.类似的问题太多……这里介绍一个 I ...

  9. python之jieba库

    jieba “结巴”中文分词:做最好的 Python 中文分词组件 "Jieba" (Chinese for "to stutter") Chinese tex ...

  10. python爬虫---selenium库的用法

    python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...