[google面试CTCI] 2-3 只给定链表中间节点指针,如何删除中间节点?
【链表】
Q:Implement an algorithm to delete a node in the middle of a single linked list, given
only access to that node
EXAMPLE
Input: the node ‘c’ from the linked list a->b->c->d->e
Result: nothing is returned, but the new linked list looks like a->b->d->e
题目:实现算法来删除单链表中的中间节点(只知道指向该节点(中间节点)的指针)。
例如:
输入:链表 a->b->c->d->e中指向节点c的指针
输出:无返回值,但新链表变为a->b->d->e
解答:
这是一个尼玛坑爹的解法,刚开始想了好久没想出来,看了提示才知道解法的。这里用到了一个小技巧。要删除中间节点,但是我们不知道要删除节点的上一个节点p,所以无法通过修改指针的方法(p->next=del->next)来删除节点,但知道要删除节点的后一个节点,那么我们换一个思路,把要删除的节点的数据与该节点的后一个节点的数据交换,然后删除后一个节点,从而达到目的。但是该方法不能删除最后一个节点,原因显而易见。
// a tricky solution,can't delete the last one element
int delete_node(NODE* node) {
int data;
NODE *p=node->next;
node->data=p->data;
node->next=p->next;
free(p);
}
作者:Viidiot 微信公众号:linux-code
[google面试CTCI] 2-3 只给定链表中间节点指针,如何删除中间节点?的更多相关文章
- [google面试CTCI] 2-0.链表的创建
创建链表.往链表中插入数据.删除数据等操作,以单链表为例. 1.使用C语言创建一个链表: typedef struct nd{ int data; struct nd* next; } node; / ...
- [google面试CTCI] 2-1.移除链表中重复元素
[链表] Q:Write code to remove duplicates from an unsorted linked list FOLLOW UP How would yo ...
- 单向链表在O(1)时间内删除一个节点
说删链表节点,第一时间想到就是遍历整个链表,找到删除节点的前驱,改变节点指向,删除节点,但是,这样删除单链表的某一节点,时间复杂度就是O(n),不符合要求: 时间复杂度是O(n)的做法就不说了,看看O ...
- [google面试CTCI] 2-2 找出链表的倒数第n个节点元素
[链表] Q:Implement an algorithm to find the nth to last element of a singly linked list . 题目:找出链表的倒数第 ...
- [google面试CTCI] 1-8.判断子字符串
[字符串与数组] Q:Assume you have a method isSubstring which checks if one word is a substring of another G ...
- [google面试CTCI] 1-4.判断两个字符串是否由相同字符组成
[字符串与数组] Q:Write a method to decide if two strings are anagrams or not 题目:写一个算法来判断两个字符串是否为换位字符串.(换位字 ...
- [google面试CTCI]1-3.字符串去重
[字符串与数组] Q:Design an algorithm and write code to remove the duplicate characters in a string without ...
- [google面试CTCI] 1-7.将矩阵中特定行、列置0
[字符串与数组] Q:Write an algorithm such that if an element in an MxN matrix is 0, its entire row and colu ...
- [google面试CTCI] 1-6.图像旋转问题
[字符串与数组] Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, wr ...
随机推荐
- 无废话WCF入门教程二[WCF应用的通信过程]
一.概述 WCF能够建立一个跨平台的安全.可信赖.事务性的解决方案,是一个WebService,.Net Remoting,Enterprise Service,WSE,MSMQ的并集,有一副很经典的 ...
- 教你一步一步部署.net免费空间OpenShift系列之三------上传ASP.net程序
接上回书,创建应用后,我们如何将自己的ASP.Net部署到应用空间呢,这里用WinSCP的SFTP协议进行上传和下载 上传ASP.net程序 下载WinSCP,并打开PuTTYGen 点击Genera ...
- 【百度地图API】建立全国银行位置查询系统(四)——如何利用百度地图的数据生成自己的标注
原文:[百度地图API]建立全国银行位置查询系统(四)--如何利用百度地图的数据生成自己的标注 摘要: 上一章留个悬念,"如果自己没有地理坐标的数据库,应该怎样制作银行的分布地图呢?&quo ...
- JavaScript两种方法来定义一个函数
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- struts2文件下载 <result type="stream">
<!--struts.xml配置--> <action name="download" class="com.unmi.action.DownloadA ...
- Critical thinking and Thoughtful writing
Critical thinkers are able to : Articulate their ideas clearly and persuasively in writing Understan ...
- Asp.net 4.0,首次请求目录下的文件时响应很慢
原文:Asp.net 4.0,首次请求目录下的文件时响应很慢 1. 问题起因2. 尝试过的处理思路3. 解决方法 1. 问题起因 一个从VS2003(.Net Framework 1.1)升级到.ne ...
- 硬盘安装Archlinux「2013-12-26」
按照Archlinux的中文WIKI安装完成,最后安装引导失败.原因未知. 折腾的脑袋好大,本来都要放弃了,幸好在贴吧发帖求助,吧友@atmouse耐心热心的帮助 最后重启成功启动.帖子地址:http ...
- CentOS 7 结构体GCC 4.8.2 32位编译环境
centos 7 结构体gcc 32位编译环境 1介绍 1.1背景 学习新 C++ 2011和C11标准. 1.2使用软件 CentOS 7(Linux version 3.10.0-123.el7. ...
- UVA10537 Toll! Revisited
difkstra + 路径输出 The Toll! Revisited Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...