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 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]相关推荐
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 { ...
- 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 ...
- Leet Code OJ 刷题顺序参考
出现频度为5: 1. Leet Code OJ 1. Two Sum [Difficulty: Easy] 2. Leet Code OJ 8. String to Integer (atoi) ...
- C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...
最新文章
- Python学习小甲鱼视频003
- php判断值是否为空
- 【开发环境】安装 Visual Studio Ultimate 2013 开发环境 ( 下载软件 | 安装软件 | 运行软件 )
- 浏览器登陆时纪录自动登陆时限
- 如何查找UI5框架的BSP应用名称
- c语言sin程序怎么写_用数控铣床加工六芒星,程序应该怎么写?
- (转)goldengate 复制进程replicat出现ORA-01403 错误
- 用mescroll实现无限上拉增加数据,下拉刷新数据 (学习笔记)
- C程序设计语言现代方法17:指针的高级应用
- AE “每用户订阅上的所有人SID 不存在”
- java语句while主意点
- autohotkey -- AHK 替换 4 为 $ 方便脚本编写
- 读书笔记_《深度学习与计算机视觉》.叶韵 编著.田疆 西门子高级研究员 作序.机械工业出版社
- flask序列化框架之marshmallow详解
- 当前时间转换成日期格式
- springboot实现邮箱接收验证码
- 【matlab】几个混沌系统时间序列数据的Matlab程序(chen、logistic、Lorenz、rossrossler)
- c android显示gif动画,MFC显示GIF动画图片
- win10有自带测试软件吗,Win10体检自带诊断工具在哪里打开 Win10关闭开机检测硬盘步骤...
- 腾讯研发“未成年防沉迷”系统,手游的社会责任之路已开启