[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 ...
随机推荐
- EasyUI基础入门之Resiable(可缩放)
easyui的base插件学习已经进行到Resizable(可缩放)了.照旧看看easyui官网的API. Resiable 正如其字面意思一样(可伸缩),resiable主要是将一些html元素扩展 ...
- Android Application.java以及它的作用
What is Application Application和Activity,Service一样是android框架的一个系统组件,当android程序启动时系统会创建一个 application ...
- Linux下Nagios
Linux下Nagios的安装与配置 一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机 ...
- 【高德地图API】从零开始学高德JS API(二)地图控件与插件——测距、圆形编辑器、鼠标工具、地图类型切换、鹰眼鱼骨
原文:[高德地图API]从零开始学高德JS API(二)地图控件与插件——测距.圆形编辑器.鼠标工具.地图类型切换.鹰眼鱼骨 摘要:无论是控件还是插件,都是在一级API接口的基础上,进行二次开发,封装 ...
- C控制语句--分支和跳转
/*C控制语句--分支和跳转*/ /*关键字 if else switch continue break case default goto 运算符:&&(且) ||(或) ?:(三元 ...
- 深入Objective-C的动态特性 Runtime
Objective-C具有相当多的动态特性,基本的,也是经常被提到和用到的有动态类型(Dynamic typing),动态绑定(Dynamic binding)和动态加载(Dynamic loadin ...
- Yii Framework2.0开发教程(4)在yii中定义全局变量
在yii中定义全局变量最好的地方是入口脚本处.也就是web目录中的index.php文件 比如我们在defined('YII_ENV') or define('YII_ENV', 'dev');后写上 ...
- 泛型委托及委托中所涉及到匿名方法、Lambda表达式
泛型委托及委托中所涉及到匿名方法.Lambda表达式 引言: 最初学习c#时,感觉委托.事件这块很难,其中在学习的过程中还写了一篇学习笔记:委托.事件学习笔记.今天重新温故委托.事件,并且把最近学习到 ...
- angular.js的一点理解
对angular.js的一点理解 2015-01-14 13:18 by MrGeorgeZhao, 317 阅读, 4 评论, 收藏, 编辑 最近一直在学习angular.js.不得不说和jquer ...
- MonkenRunner通过HierarchyViewer定位控件的方法和建议(Appium/UIAutomator/Robotium姊妹篇)
1. 背景 在使用MonkeyRunner的时候我们经常会用到Chimchat下面的HierarchyViewer模块来获取目标控件的一些信息来辅助我们测试,但在MonkeyRunner的官网上是没有 ...