题目:

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相关推荐

  1. 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 ...

  2. 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 ...

  3. 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 ...

  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 t ...

  5. Xiaohe-LeetCode 237 Delete Node in a Linked List

    This is almost the best solution.16ms. /** * Definition for singly-linked list. * struct ListNode { ...

  6. C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...

  7. 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 ...

  8. java中删除node节点_[Java]LeetCode237. 删除链表中的节点 | Delete Node in a Linked List

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  9. [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 ...

最新文章

  1. Hadoop的启动顺序和停止顺序
  2. UVa 10420 List of Conquests
  3. ORACLE系列异常总结ORA
  4. Python+Requests+Pytest+YAML+Allure实现接口自动化
  5. angular组件--tips提示功能
  6. Day1 - Python基础1
  7. (实用软件分享)屏幕取色器ColorPix
  8. 学了java然后深入_JAVA8学习——从源码角度深入Stream流(学习过程)
  9. Failed to connect to database. Maximum number of connections to instance exceeded
  10. VS2017安装教程
  11. Flutter 判断横竖屏(含尺寸、方向改变时触发)
  12. 用python编写猜数字游戏
  13. 同一个图表创建双坐标轴,显示多个图例
  14. 中文文案排版指北(转自GitHub)
  15. 李彦宏:我不是传奇(网络转载)
  16. 网络资产扫描工具 -- Goby
  17. mac打不开txt文件,出现“文本编码“中文 (GB 18030)”不适用。”的解决办法
  18. linux桌面temp,Linux系统之ubuntu桌面系统使用探索[temprature]
  19. android私密照片恢复,如何恢复手机删除照片? 超实用操作方法!
  20. java英文不好可以学吗_英语不好可以学好Java吗?

热门文章

  1. spring boot 异常汇总
  2. vuejs兄弟组件之间的通信
  3. Spring AOP用法
  4. 性能测试培训:定位jvm耗时函数
  5. Spring初学(一)
  6. apache的斜杠问题
  7. 一次thinkphp框架 success跳转卡顿问题的解决
  8. 需求实在太旺盛,三星电子考虑扩大在华芯片产能
  9. jconsole命令(Java Monitoring and Management Console)
  10. 典型的Linux系统启动需要完成的服务