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 that node.
Supposed the linked list is 1 -> 2 -> 3 -> 4
and you are given the third node with value 3
, the linked list should become 1 -> 2 -> 4
after calling your function.
链接: http://leetcode.com/problems/delete-node-in-a-linked-list/
题解:
没啥可说的,就是干!
Time Complexity - O(1), Space Complexity - in place
/*** 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;} }
二刷:
就是改变当前node的val和next节点,都变成下一个节点的值和reference就可以了。
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) {if (node == null || node.next == null) {return;}node.val = node.next.val;node.next = node.next.next;} }
三刷:
唔,这道题我也刷了三遍...
因为不是delete tail,意味着当前节点非空,并且下一个节点非空。 所以我们可以略去一些边界条件的判断。
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;} }
转载于:https://www.cnblogs.com/yrbbest/p/5003811.html
237. Delete Node in a Linked List相关推荐
- LeetCode刷题记录3——237. Delete Node in a Linked List(easy)
LeetCode刷题记录3--237. Delete Node in a Linked List(easy) 目录 LeetCode刷题记录3--237. Delete Node in a Linke ...
- 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 t ...
- Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]
题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...
- 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 t ...
- Xiaohe-LeetCode 237 Delete Node in a Linked List
This is almost the best solution.16ms. /** * Definition for singly-linked list. * struct ListNode { ...
- C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...
- Jan 12 - Delete Node in a Linked List; Data Structure; Linked List; Pointer;
代码: /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* List ...
- java中删除node节点_[Java]LeetCode237. 删除链表中的节点 | Delete Node in a Linked List
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...
最新文章
- Hadoop的启动顺序和停止顺序
- UVa 10420 List of Conquests
- ORACLE系列异常总结ORA
- Python+Requests+Pytest+YAML+Allure实现接口自动化
- angular组件--tips提示功能
- Day1 - Python基础1
- (实用软件分享)屏幕取色器ColorPix
- 学了java然后深入_JAVA8学习——从源码角度深入Stream流(学习过程)
- Failed to connect to database. Maximum number of connections to instance exceeded
- VS2017安装教程
- Flutter 判断横竖屏(含尺寸、方向改变时触发)
- 用python编写猜数字游戏
- 同一个图表创建双坐标轴,显示多个图例
- 中文文案排版指北(转自GitHub)
- 李彦宏:我不是传奇(网络转载)
- 网络资产扫描工具 -- Goby
- mac打不开txt文件,出现“文本编码“中文 (GB 18030)”不适用。”的解决办法
- linux桌面temp,Linux系统之ubuntu桌面系统使用探索[temprature]
- android私密照片恢复,如何恢复手机删除照片? 超实用操作方法!
- java英文不好可以学吗_英语不好可以学好Java吗?