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. 简述at和crontab命令

    at 在指定时间执行相关命令 用法:at [option] TIME 常用选项: -l:查询等待运行作业的队列 -d:删除作业,加作业号 -q QUEVE: -c :查看具体作业任务: -f /pat ...

  2. 安装grub到U盘分区,实现多系统引导

    目录 1.分区工具及分区类型 1.1 显示分区表和分区信息 1.1.1 fdisk -l 1.1.2 gdisk -l 1.1.3 parted -l 1.2 常见分区类型 1.3 分区样例 1.3. ...

  3. javascript xml字符串转为json对象

    var xmlStr = '<?xml version="1.0" encoding="utf-8" ?><config><nam ...

  4. HDFS基本Shell命令

    bin目录下: 1. hadoop fs 基本操作命令,类似linux shell 2. hadoop dfsadmin    管理命令 3. hadoop fsck (1. 检查hdfs中文件的健康 ...

  5. SQL group_concat find_in_set 的使用

    SELECT p.id as pid,p.code as code,p.topic_name topic_name,p.vm_id as vm_id,GROUP_CONCAT(iso.iso_name ...

  6. 找出n之内的完全数, 并输出其因子

    定义: 完全数:所有的真因子(即除了自身以外的约数)的和,恰好等于它本身.例如:第一个完全数是6,它有约数1.2.3.6,除去它本身6外,其余3个数相加,1+2+3=6.第二个完全数是28,它有约数1 ...

  7. lay-verify 无效

    lay-verify 无效 使用lay-verify有两个需要注意的地方: form标签需要添加 class="layui-form" 提交按钮需要添加 lay-submit=&q ...

  8. homework 张一刚

    #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h& ...

  9. bootice-diskinfo参数

    http://bbs.wuyou.net/forum.php?mod=redirect&goto=findpost&ptid=57675&pid=3023573&fro ...

  10. python爬虫学习笔记(二)——基础篇之爬虫基本原理

    1.什么是爬虫? 请求网站并提取数据的自动化程序 2.爬虫基本流程 2.1发起请求 通过HTTP库向目标站点发起请求,即发起一个Request,请求可以包含额外的headers等信息,等待服务器响应: ...