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. 使用Impersonation仿冒用户运行WCF 服务方法

    默认情况下,当我们把wcf服务器部署在IIS上时, 我们访问wcf时使用的是IIS内建的内建角色,对于IIS8.0,它被命名为ApplicationPoolIdentity, 一般来说,系统为每个应用 ...

  2. 【TortoiseGit】TortoiseGit将本地库push到远端

    以前也在使用GitHub,2年前电脑上就装了TortoiseGit和SVN,公司也在用Git,但是并没有刻意去做一些事情,未免觉得有些生疏,今天闲来无聊.玩了一把.[做中成长] 对于GitToiseG ...

  3. 小波包分解 仿真 matlab

    clc;close all;clear;fs = 100000;t = 1: 100;x = sin(2*pi*4000* t/fs) + sin(2*pi*40000*t/fs); %db8[Lo_ ...

  4. Http与Socket小谈

    http与socket是网络编程中最为重要的概念,不管是客户端还是服务端,都是最为重要的部分,以下简述两者的关系和区别(个人见解). Http 定义 基于应用层的超文本传输协议.通常承载于TCP/IP ...

  5. MYsql 数据库密码忘记(Linux)

    在Linux 上面装上了 Mysql 数据库,但是发现密码忘了,悲催,解决方法跟Window系统下一样的, 不管是哪个操作系统处理的思路是相同的,就是首先要把mysql的权限去掉,这样即使忘了密码,不 ...

  6. there's no qt version assigned to this project for platform

    VS+Qt编译一个新建的项目报there's no qt version assigned to this project for platform xxx的错误. 解决方案: 打开Qt_vs_add ...

  7. Ubuntu和Win双系统解决主板时间差

    由于U和W两个系统的默认时间相差8小时,且会自动改变电脑主板里的默认时间,需要在U中进行设置. timedatectl set-local-rtc true

  8. UML大战需求分析——阅读笔记04

    读<UML大战需求分析>有感04 开发某系统的重要前提是: 这个系统有谁在用? 这些人通过这个系统能做什么事? 一般搞清楚这件事,再画个业务流程图,就能条例清楚的表达系统的需求了.作为一个 ...

  9. MongoDB聚合运算之mapReduce函数的使用(11)

    mapReduce 随着"大数据"概念而流行. 其实mapReduce的概念非常简单, 从功能上说,相当于RDBMS的 group 操作 mapReduce的真正强项在哪? 答:在 ...

  10. wkhtmltopdf 将网页转换为PDF和图片

    wkhtmltopdf 是一个shell工具,它使用了WebKit渲染引擎和Qt,将网页html转换为pdf的强大工具,转换后的pdf也可以通过pdf工具进行复制.备注.修改 官网下载地址:http: ...