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. SVN命令备忘录

    批量添加(先添加再上传) svn st | grep '^\?' | tr '^\?' ' ' | sed 's/[ ]*//' | sed 's/[ ]/\\ /g' | xargs svn add ...

  2. Cisco Packet Tracer7.1 rip协议实验

    设备: 路由器:三个1941:router0,router1,router2; 终端用户:二个PC-PT:PC0,PC1; 网络配置: 网络 设备 接口 IP 设备 接口 IP 192.168.0.0 ...

  3. Linux本地yum源配置以及使用yum源安装gcc编译环境

    本文档是图文安装本地yum源的教程,以安装gcc编译环境为例. 适用范围:所有的cetos,红帽,fedroa版本 适用人群:有一点linux基础的小白 范例系统版本:CentOS Linux rel ...

  4. zabbix使用邮箱告警

    目的:使用自己的邮箱(目前我使用的是腾讯企业邮箱)发送告警邮件 1.配置Email:管理->报警媒介类型->Email->修改对应Email参数 2.修改admin用户的报警媒介Em ...

  5. 宝塔linux面板 解决TP3.2 404

    在配置文件中加入一下配置: location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1; } } location ...

  6. 【备份】如何在 PADS Layout 中选择 Gerber 274X 格式

    如何在 PADS Layout 中选择 Gerber 274X 格式. 起初原因是 JLC 说 274X 和 274D 的差别. 有小伙伴使用了 274D 的格式,结果做出来的 PCB 有问题.

  7. 使用Visual Studio给SQL生成测试数据

    参考:http://www.cnblogs.com/CareySon/archive/2012/02/20/2359444.html 使用VS2010的数据生成计划来生成测试数据 以下面两个表来做例子 ...

  8. GDI与GDI+性能比较

    编写程序对GDI和GDI+绘制进行了比较,经过比较,GDI相对GDI+还是有一些性能优势的. 同时比较了每次绘制创建TGPGraphics对象和共用一个TGPGraphics对象的情况,两者性能相差不 ...

  9. win10 家庭版 升级 win10企业版

    更改秘钥 我的电脑(右键)->属性-> 更改产品秘钥 -> 96YNV-9X4RP-2YYKB-RMQH4-6Q72D->重启系统 如果秘钥过期了,就百度按时间搜索,总有一个是 ...

  10. bash: ./LM35_make_fs: Permission denied 解决办法

    执行命令的时候 ./LM35_make_fs 遇到 permission denied, bash: ./LM35_make_fs: Permission denied权限的问题,可以运行 ls -l ...