编写一个函数,在给定单链表一个结点(非尾结点)的情况下,删除该结点。

假设该链表为1 -> 2 -> 3 -> 4 并且给定你链表中第三个值为3的节点,在调用你的函数后,该链表应变为1 -> 2 -> 4。

详见:https://leetcode.com/problems/delete-node-in-a-linked-list/description/

Java实现:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
node.val=node.next.val;
node.next=node.next.next;
}
}

C++实现:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
node->val=node->next->val;
ListNode *tmp=node->next;
node->next=tmp->next;
delete tmp;
}
};

  

237 Delete Node in a Linked List 删除链表的结点的更多相关文章

  1. LeetCode 237. Delete Node in a Linked List 删除链表结点(只给定要删除的结点) C++/Java

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

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

  3. LeetCode #237. Delete Node in a Linked List 删除链表中的节点

    https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 非常巧妙的一道题. 题目没有给head,心想没有head我怎么才能找到要删 ...

  4. [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

    2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...

  5. [LeetCode] 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 ...

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

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

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

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

随机推荐

  1. POJ 1804 逆序对数量 / 归并排序

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12175   Accepted: 6147 Descrip ...

  2. HDU4325 树状数组+离散化

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Flowers Time Limit: 4000/2000 MS (Java/Others)   ...

  3. Layui图标

    layui 图标 layui 的所有图标全部采用字体形式,取材于阿里巴巴矢量图标库(iconfont).因此你可以把一个 icon 看作是一个普通的文字,这意味着你直接用 css 控制文字属性,如 c ...

  4. POJ1068 Parencodings 解题报告

    Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...

  5. MongoDB小结07 - update【$pop】

    如果将数组看做队列,可以用$pop方法删除第一个或者最后一个元素 {$pop:{"key":-1}},{$pop:{"key":1}}

  6. Windows7下ftp服务器

    1. 创建用户 2. 创建ftp服务 3. 管理ftp站点 4. 权限编辑 5. 绑定iP和端口 6. 访问 7. Java代码上传文件 import java.io.File; import jav ...

  7. web 开发之js---js获取select标签选中的值

    var obj = document.getElementByIdx_x(”testSelect”); //定位id var index = obj.selectedIndex; // 选中索引 va ...

  8. easyUI的treegrid添加节点(append)时间过长,设置等待(wait)遮罩效果

    如题所述,在treegrid中,一次添加多个节点时,时间很长,但easyUI并无显示等待信息,让人以为是陷入了死循环,或者死机了,用户体验很差. 本来,treegrid(或者datagrid)有所谓的 ...

  9. HttpClient-03Http状态管理

    最初,Http被设计成一个无状态的,面向请求/响应的协议,所以它不能在逻辑相关的http请求/响应中保持状态会话.由于越来越多的系统使用http协议,其中包括http从来没有想支持的系统,比如电子商务 ...

  10. NinePatchChunk.java分析

    最近在Swing中使用.9图片,用到了NinePatchChunk.java文件,但是发现有时会出现无法完美展示的情况,决定修复一下这个问题,顺便研究一些.9的绘制过程 通过分析发现draw函数先是计 ...