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

假设该链表为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. linux 用户管理、权限管理

    1.useradd -[ugGdsce]2.passwd 用户名 ================================================ 1.chmod 2.chown 3. ...

  2. Elasticsearch5.6搭建及拼音中文混合搜索实现

    https://blog.csdn.net/UUfFO/article/details/78154499

  3. FZU Problem 2171 防守阵地 II (线段树区间更新模板题)

    http://acm.fzu.edu.cn/problem.php?pid=2171 成段增减,区间求和.add累加更新的次数. #include <iostream> #include ...

  4. 一个有趣的问题:ls -l显示的内容中total究竟是什么?

    当我们在使用ls -l的命令时,我们会看到例如以下类似的信息. 非常多人可能对于第一行的total 12的数值并非非常在意,可是你是否想过,它到底是什么意思? man中的说明,我们能够看出total的 ...

  5. 量化分析师的Python日记【第1天:谁来给我讲讲Python?】

    量化分析师的Python日记[第1天:谁来给我讲讲Python?]薛昆Kelvin优矿 001 号员工2015-01-28 15:48 58 144克隆 ###“谁来给我讲讲Python?” 作为无基 ...

  6. Python常用数据处理函数

    1.基本统计特征函数 方法名 函数功能 所属库 使用格式 sum() 计算数据样本综合(按列计算) Pandas D.sum() mean() 计算数据样本算数平均数 Pandas D.mean() ...

  7. MVC 用户权限HttpContext.User.IsInRole()

    这几天在用MVC做一个项目,用到了HttpContext.User.IsInRole() 这个方法,但是每次当我用的时候,HttpContext.User.IsInRole(“Admin”) 返回的永 ...

  8. 《ASP.NET4 从入门到精通》学习笔记4

    第4部分诊断与插件 刚開始看这章的时候,真实一头雾水.不知道在讲什么.只是看了关于http pipeline之后.才了解相关说明. 因此对于这一章的学习,建议各位首先看看http pipeline然后 ...

  9. 【bzoj2748】[HAOI2012]音量调节

    设F[i][j]表示在第i首歌曲结束后,音量能否刚好为j 转移:F[i][j]=F[i][j-C[i]] or F[i][j+C[i]] 初始化:F[0][beginlevel]=true 最后在所有 ...

  10. Ubuntu利用TCP协议来获取server时间

    Linux利用TCP协议来获取server时间 这里使用Unix网络编程里面的一个小程序,该client建立一个到server的TCP连接,然后读取由server以直观可读格式简单地送回的当前时间和日 ...