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.

题目意思:

  写一个函数删除单链表中的某个结点,函数只给出了删除的结点

解题思路:

  只需要将删除的结点与其下一个结点交换值即可,同时调整好链表指针

源代码:

 class Solution {
public:
void deleteNode(ListNode* node) {
if( node->next == NULL){
delete node;
node = NULL;
}
swap(node->val, node->next->val);
ListNode* nextNode = node->next;
node->next = nextNode->next;
delete nextNode;
}
};

Leetcode Delete Node in a Linked List的更多相关文章

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

  2. LeetCode——Delete Node in a Linked List

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

  3. LeetCode Delete Node in a Linked List (删除链表中的元素)

    题意:给一个将要删除的位置的指针,要删除掉该元素.被删元素不会是链尾(不可能删得掉). 思路:将要找到前面的指针是不可能了,但是可以将后面的元素往前移1位,再删除最后一个元素. /** * Defin ...

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

  5. 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements

    237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...

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

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

  8. LeetCode_237. Delete Node in a Linked List

    237. Delete Node in a Linked List Easy Write a function to delete a node (except the tail) in a sing ...

  9. 【4_237】Delete Node in a Linked List

    Delete Node in a Linked List Total Accepted: 48121 Total Submissions: 109297 Difficulty: Easy Write ...

随机推荐

  1. Object-C目录

    Object学习目录: 1.OC概述 2.OC第一个应用程序 3.OC之类和对象(属性.方法,点语法) 4.OC之类的扩充(self,super关键字.继承.构造函数等)   5.OC之分类Categ ...

  2. 在XP上运行IIS5.1新建站点

    系统问题,XP下IIS5.1不能直接新建站点,因为内核限制只能同时运行一个站点,要想新建站点,必须把当前站点停掉,然后用adsutil.vbs脚本创建,脚本在C:\Inetpub\AdminScrip ...

  3. Maven pom.xml 元素配置说明(一)

    部分来源: Maven中 dependencies 节点和 dependencyManagement 节点的区别 dependencies与dependencyManagement的区别 maven ...

  4. JS客户端RSA加密,Java服务端解密

    常用语网页客户端对密码加密,在后端java解密还原 java代码依赖    <dependency>      <groupId>commons-codec</group ...

  5. 本周psp个人作业

    计划--用一天的时间来做这个项目 需求分析--作为一个观众,我想要知道每局的比分,以便我更了解比赛情况. 生成设计文档--用类图来进行说明. 设计复审---无 代码规范--3H 具体设计--建立数据库 ...

  6. "_OBJC_CLASS_$_CMMotionManager", referenced from:

    好久没写随笔了,今日项目爆红.如下: 缺少系统库 CoreMotion.framework, 在Build Phases -> Link Binary With Libraries 中添加即可.

  7. MFC---给按钮加上快捷键

    现在快捷键的使用已经很频繁了.快捷键可以使我们的操作变得更简单,更快捷.如何给自己的按钮加一个快捷键呢.    如下图:我们希望给我们的参照按钮加一个快捷键CTR + F. 不要以为在按钮的标题上加上 ...

  8. JAVA的模式对话框和非模式对话框

    周末的时候,一位网友让我帮他把他的无模式对话框改成有模式对话框. 界面是由swing制作的,都是JFrame,我从来没有接触过swing编程.大致的代码还是看的懂,很多都和C#很相似. 然后就去查资料 ...

  9. Physics(物理系统)

    物理: Physics            Box2d   Unity 内置NVDIA PhysX物理引擎 刚体:要使一个物体在物理控制下,简单添加一个刚体给它.这时,物体将受重力影响,并可以与其他 ...

  10. Java研发岗位面试归类A(附答案)

    题目来自http://www.codeceo.com/article/201-java-interview-qa.html,答案自己网上找的,如有疏漏,欢迎斧正.一起学习,共同进步. 一.Java基础 ...