【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
目录
[LeetCode]
题目地址:https://leetcode.com/problems/delete-node-in-a-linked-list/
Total Accepted: 78258 Total Submissions: 179086 Difficulty: Easy
题目描述
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:
令node.val = node.next.val,node.next = node.next.next即可
Java代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void deleteNode(ListNode node) {
node.val=node.next.val;
node.next=node.next.next;
}
}
python代码如下:
# 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
C++代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val;
node->next = node->next->next;
}
};
日期
2016/4/30 0:39:40
2018 年 11 月 11 日 —— 剁手节快乐
2019 年 9 月 27 日 —— 昨天面快手,竟然是纯刷题
【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- (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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- SQL-join(inner join)、left join、right join、full join
0.JOIN 类型 有时我们需要从两个或更多的表中获取结果,数据库中的表可通过键将彼此联系起来.每个表中都有一个主键,主键(Primary Key)是一个列,值都唯一.这样做的目的是在不重复每个表中的 ...
- 学习Java的第十八天
一.今日收获 1.java完全学习手册第三章算法的3.1比较值 2.看哔哩哔哩上的教学视频 二.今日问题 1.在第一个最大值程序运行时经常报错. 2.哔哩哔哩教学视频的一些术语不太理解,还需要了解 三 ...
- 1小时学会Git玩转GitHub
版权声明:原创不易,本文禁止抄袭.转载,侵权必究! 本次教程建议一边阅读一边用电脑实操 目录 一.了解Git和Github 1.1 什么是Git 1.2 什么是版本控制系统 1.3 什么是Github ...
- 『学了就忘』Linux启动引导与修复 — 68、Linux系统运行级别
目录 1.Linux系统运行级别介绍 2.查看运行级别 3.修改当前系统的运行级别 4.系统默认运行级别 5./etc/rc.d/rc.local文件说明 1.Linux系统运行级别介绍 Linux默 ...
- Hive(一)【基本概念、安装】
目录 一. Hive基本概念 1.1 Hive是什么 1.2 Hive的优缺点 1.3 Hive的架构 1.4 Hive和数据库的区别 二. Hive安装 2.1 安装地址 2.2 Mysql的安装 ...
- 【STM8】STM8S介绍(编程环境、烧录、芯片内容)(Vcap需要一个电容接地)
这篇博客的介绍大纲 [1]我使用的开发板和烧录器 [2]编程环境 [3]烧录软件和界面 [4]芯片内容 [1]我使用的开发板和烧录器 首先,我用的是STM8S003F3P6这款开发板,淘宝上就有了,5 ...
- iOS11&IPhoneX适配
1.在iOS 11中,会默认开启获取的一个估算值来获取一个大体的空间大小,导致不能正常显示,可以选择关闭.目前尝试在delegate中处理不能很好的解决,不过可以直接设置: Swift if #ava ...
- 浅谈iptables与firewalld防火墙
iptables基于包过滤的防火墙工具 ,Linux 内核集成的 IP 信息包过滤系统,对流入和流出服务器的数据包进行精细管理 规则是存储在专用信息包过滤表中 防火墙按照规则做出判断 而netfilt ...
- vueAPI (data,props,methods,watch,computed,template,render)
data Vue 实例的数据对象.Vue 将会递归将 data 的属性转换为 getter/setter,从而让 data 的属性能够响应数据变化.实例创建之后,可以通过vm.$data来访问原始数据 ...
- 【力扣】82. 删除排序链表中的重复元素 II
存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字. 返回同样按升序排列的结果链表. 示例 1: 输入:hea ...