题目:
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.

翻译:
写一个方法去删除单链表的一个节点(非尾节点),只给出访问这个节点的参数。

假定链表是1 -> 2 -> 3 -> 4,给你的节点是第三个节点(值为3),这个链表在调用你的方法后应该变为 1 -> 2 -> 4。

分析:
删除链表的节点的常规做法是修改上一个节点的next指针。然而这边并没有提供上一个节点的访问方式,故转变思路改为删除下一节点,直接将下一个节点的val和next赋值给当前节点。

代码:

/*** 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;}
}

Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]相关推荐

  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. Leet Code OJ 107. Binary Tree Level Order Traversal II [Difficulty: Easy]

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

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

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

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

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

  7. Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...

  8. Leet Code OJ 刷题顺序参考

    出现频度为5:  1. Leet Code OJ 1. Two Sum [Difficulty: Easy]  2. Leet Code OJ 8. String to Integer (atoi) ...

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

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

最新文章

  1. Python学习小甲鱼视频003
  2. php判断值是否为空
  3. 【开发环境】安装 Visual Studio Ultimate 2013 开发环境 ( 下载软件 | 安装软件 | 运行软件 )
  4. 浏览器登陆时纪录自动登陆时限
  5. 如何查找UI5框架的BSP应用名称
  6. c语言sin程序怎么写_用数控铣床加工六芒星,程序应该怎么写?
  7. (转)goldengate 复制进程replicat出现ORA-01403 错误
  8. 用mescroll实现无限上拉增加数据,下拉刷新数据 (学习笔记)
  9. C程序设计语言现代方法17:指针的高级应用
  10. AE “每用户订阅上的所有人SID 不存在”
  11. java语句while主意点
  12. autohotkey -- AHK 替换 4 为 $ 方便脚本编写
  13. 读书笔记_《深度学习与计算机视觉》.叶韵 编著.田疆 西门子高级研究员 作序.机械工业出版社
  14. flask序列化框架之marshmallow详解
  15. 当前时间转换成日期格式
  16. springboot实现邮箱接收验证码
  17. 【matlab】几个混沌系统时间序列数据的Matlab程序(chen、logistic、Lorenz、rossrossler)
  18. c android显示gif动画,MFC显示GIF动画图片
  19. win10有自带测试软件吗,Win10体检自带诊断工具在哪里打开 Win10关闭开机检测硬盘步骤...
  20. 腾讯研发“未成年防沉迷”系统,手游的社会责任之路已开启

热门文章

  1. 大量的QT控件及示例发放
  2. 棋牌游戏服务器架构: 总体设计
  3. cocos2d-x游戏开发(八)各类构造器
  4. 消息中间件(Kafka/RabbitMQ)收录集
  5. 如何构建一套高性能、高可用性、低成本的视频处理系统?
  6. 慌!还不了解Java中的分支预测?!
  7. Shell的基本语法结构
  8. 走近互联网先驱者——Henning Schulzrinne
  9. 多媒体技术PI 第三期—网络传输线上圆桌
  10. 音视频技术开发周刊 | 181