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]的更多相关文章
- 237. Delete Node in a Linked List【easy】
237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...
- 237. Delete Node in a Linked List(C++)
237. Delete Node in a Linked Lis t Write a function to delete a node (except the tail) in a singly l ...
- LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List
283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...
- [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_Easy tag: 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 ...
- 【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 ...
- 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 ...
随机推荐
- 2017-3-01 test
三道好像都是HDU上的题QAQ 题目名称都没改,差评 T1:http://acm.hdu.edu.cn/showproblem.php?pid=5073 被卡精度了QAQ 先排一发序,然后发现最后未动 ...
- github的一些简单用法
关于 项目 上传 大多数人都是使用命令行上传 步骤分为以下几步: 在github上创建你的 repositories ->github.com - >右下角 new reopsi ...
- linux之函数
17.1 基本的脚本函数 函数:是一个脚本代码块,可以为其命名并在代码中任何位置重用. 17.1.1 创建函数 有两种格式:name 是函数名 1) function name { ...
- BZOJ 3513: [MUTC2013]idiots
3513: [MUTC2013]idiots Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 476 Solved: 162[Submit][Stat ...
- GMM 的EM 实现
算法逻辑在这里: http://www.cnblogs.com/Azhu/p/4131733.html 贴之前先说下,本来呢是打算自己写一个的,在matlab 上,不过,实在是写不出来那么高效和健壮的 ...
- android实现多条件筛选列表菜单筛选菜单
封装组合控件实现一个简单的多条件筛选菜单,可根据自己需求定制筛选条件,动态添加筛选项,灵活使用. 控件封装,点击切换,使用popupWindow实现下拉列表,项目中封装了多种数组数据排序处理方法的工具 ...
- ASP.NET MVC 利用Razor引擎生成静态页
实现原理及步骤: 1.通过ViewEngines.Engines.FindView查找到对应的视图,如果是部分视图,则用:ViewEngines.Engines.FindPartialView: 2. ...
- linux查看hostname以及修改hostname
查看hostname : hostname 修改hostname : hostnamectl set-hostname master (比如要修改为master) 修改完重启生效 : ...
- workflow engine Ruote初体验之一(概念)
由于最近自己写点小东西,需要有工作流程管理方面的应用,所有的环境为Ruby on rails,所有在选择流程引擎的时候选择了ruote,但是对于ruote是完全陌生的,所以在这里记下点滴,如果理解的不 ...
- An Introduction to Reflection in C++
Apr 13, 2017 Stop me if you’ve heard this one before. You are working on a messaging middleware, a g ...