【链表】

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 只给定链表中间节点指针,如何删除中间节点?的更多相关文章

  1. [google面试CTCI] 2-0.链表的创建

    创建链表.往链表中插入数据.删除数据等操作,以单链表为例. 1.使用C语言创建一个链表: typedef struct nd{ int data; struct nd* next; } node; / ...

  2. [google面试CTCI] 2-1.移除链表中重复元素

    [链表] Q:Write code to remove duplicates from an unsorted linked list      FOLLOW UP      How would yo ...

  3. 单向链表在O(1)时间内删除一个节点

    说删链表节点,第一时间想到就是遍历整个链表,找到删除节点的前驱,改变节点指向,删除节点,但是,这样删除单链表的某一节点,时间复杂度就是O(n),不符合要求: 时间复杂度是O(n)的做法就不说了,看看O ...

  4. [google面试CTCI] 2-2 找出链表的倒数第n个节点元素

    [链表] Q:Implement an algorithm to find the nth to last element of a singly  linked list . 题目:找出链表的倒数第 ...

  5. [google面试CTCI] 1-8.判断子字符串

    [字符串与数组] Q:Assume you have a method isSubstring which checks if one word is a substring of another G ...

  6. [google面试CTCI] 1-4.判断两个字符串是否由相同字符组成

    [字符串与数组] Q:Write a method to decide if two strings are anagrams or not 题目:写一个算法来判断两个字符串是否为换位字符串.(换位字 ...

  7. [google面试CTCI]1-3.字符串去重

    [字符串与数组] Q:Design an algorithm and write code to remove the duplicate characters in a string without ...

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

  9. [google面试CTCI] 1-6.图像旋转问题

    [字符串与数组] Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, wr ...

随机推荐

  1. [Android] Upload package to device fails #2720

    错误描述: 解决办法:   来源:https://facebook.github.io/react-native/docs/android-setup.html

  2. 至尊快速,国产语言RPP 1.83强势来袭

    以下是 R++的性能測试数据:(奔腾 1.86GHZ,測试 3 次取平均值) 执行效率: R++的内部结构和 C++大致同样,所以理论上 R++能够达到和 C++一样的执行速度,眼下 R++已开启汇编 ...

  3. 笔记28 mssql的update :from语法

    原文:笔记28 mssql的update :from语法 笔记28 mssql的update :from语法 --mssql的update :from语法 --a表 b表 结构分别 id ,name ...

  4. JDBC加载过程

    jdbc载入的过程如图所看到的. 桥接模式请參照:设计模式:桥接模式 blog目的:与图说话 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  5. dojo/request

    dojo/request模块整体架构解析   总体说明 做前端当然少不了ajax的使用,使用dojo的童鞋都知道dojo是基于模块化管理的前端框架,其中对ajax的处理位于dojo/request模块 ...

  6. 【Espruino】NO.15 nRF24L01+无线收发器

    http://blog.csdn.net/qwert1213131/article/details/35853747 本文属于个人理解,能力有限,纰漏在所难免,还望指正! [小鱼有点电] [Espru ...

  7. 让Sqlite脱离VC++ Runtime独立执行

    前段时间在开发OrayTalk(傲瑞通企业即时通信系统)的聊天记录模块时用到了Sqlite,这是我第一次接触和使用Sqlite,整体感觉还是很不错的.这里把我使用Sqlite的经验跟大家分享一下. 一 ...

  8. DBUtils的使用

    DButils是apache旗下Commons项目中的一个JDBC工具包,它可以为帮助我们简化对JDBC的操作,但它并不是一个ORM框架,只是可以为我们执行sql,并将返回的ResultSet转化成我 ...

  9. sql优化的50中方法

    查询速度慢的原因很多,常见如下几种:    1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)    2.I/O吞吐量小,形成了瓶颈效应.    3.没有创建计算列导致查询不优化 ...

  10. C语言编写Windows服务程序

    原文:C语言编写Windows服务程序 #include <Windows.h> #include <stdio.h> #define SLEEP_TIME 5000 // 间 ...