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. 最详细最权威的Android 编码规范

    1. 前言 这份文档参考了 Google Java 编程风格规范和 Google 官方 Android 编码风格规范.该文档仅供参考,只要形成一个统一的风格,见量知其意就可. 1.1 术语说明 在本文 ...

  2. PyCharm设置仿sublime配色__Py版本2018.3.5

    效果图: 导出settings 模板为: 1.https://files.cnblogs.com/files/xier/PyCharm_settings.zip 2.https://files.cnb ...

  3. centos tree 命令

    ftp://mama.indstate.edu/linux/tree/ download & make

  4. [双硬盘GPT分区安装linux] ----安装

    最近机器学习课程需要用到pytorch, 基于cuda8的pytorch的需要,最终决定还是要安装一个linux系统在我的windows10机器上.本篇记录安装过程备用. 本人笔记本是联想Y520,系 ...

  5. 初始化HTML样式(转载)

    方式一 ;; } body { background:#fff; color:#555; font-size:14px; font-family: Verdana, Arial, Helvetica, ...

  6. HanLP Analysis for Elasticsearch

    基于 HanLP 的 Elasticsearch 中文分词插件,核心功能: 兼容 ES 5.x-7.x: 内置词典,无需额外配置即可使用: 支持用户自定义词典: 支持远程词典热更新(待开发): 内置多 ...

  7. Javascript 使用postMessage对iframe跨域传值或通信

    实现目标:两个网站页面实现跨域相互通信 当前例子依赖于 jQuery 3.0 父页面代码:www.a.com/a.html <iframe id="myIframe" src ...

  8. 蓝桥第八届省赛 javaB组承压计算

    X星球的高科技实验室中整齐地堆放着某批珍贵金属原料. 每块金属原料的外形.尺寸完全一致,但重量不同.金属材料被严格地堆放成金字塔形. 7 5 8 7 8 8 9 2 7 2 8 1 4 9 1 8 1 ...

  9. ArcEngine 调用GP里面的Merge工具

    这两天,为ArcEngine中的Merge工具的调试伤头脑筋,明明很简单的事情,就是搞不掂儿.今天忽然想到是否是中文的关系,果然,把中文修改成英文成功了.真是快郁闷死了! public static ...

  10. 时间复杂度O()与KMP算法

    要得到某个结果,可以有很多种方式,算法就是为了寻找一条最快的方式. 而评判其好坏的标准就是时间复杂度. O(1): 我们把执行一次的时间复杂度定义为O(1) sum = a +b; cout < ...