careercup-链表 2.3
2.3 实现一个算法,删除单向链表中间的某个结点,假设你只能访问该结点。(即你不知道头结点)
这个问题的关键是你只有一个指向要删除结点的指针,如果直接删除它,这条链表就断了。 但你又没办法得到该结点之前结点的指针,是的,它连头结点也不提供。在这种情况下, 你只能另觅他径。重新审视一下这个问题,我们只能获得从c结点开始后的指针, 如果让你删除c结点后的某个结点,那肯定是没问题的。比如删除结点d,那只需要把c 的next指针指向e,然后delete d就ok了。好的,如果我们就删除结点d,我们将得到 a->b->c->e,和目标链表只差了一个结点。怎么办呢?把d的数据给c! 结点结构都是一样的,删除谁都一样,最关键的是结点中的数据,只要我们留下的是数据 a->b->d->e就OK了。
思路已经有了,直接写代码?等等,写代码之前,让我们再简单分析一 下可能出现的各种情况(当然包括边界情况)。如果c指向链表的:1.头结点;2.中间结点。 3.尾结点。4.为空。情况1,2属于正常情况,直接将d的数据给c,c的next指针指向d 的next指向所指结点,删除d就OK。情况4为空,直接返回。情况3比较特殊,如果c 指向尾结点,一般会认为直接删除c就ok了,反正c后面也没有结点了,不用担心链表断开。 可是真的是这样吗?代码告诉我们,直接删除c,指向c的那个结点(比如说b)的next指针 并不会为空。也就是说,如果你打印这个链表,它还是会打印出和原来链表长度一样的链表, 而且最后一个元素为0!
关于这一点,原书CTCI中是这么讲的,这正是面试官希望你指出来的。然后, 你可以做一些特殊处理,比如当c是尾结点时,将它的数据设置为一些特殊字符, 这样在打印时就可以不打印它。当然也可以直接不处理这种情况,原书里的代码就是这么做 的。这里,也直接不处理这种情况。
C++实现代码:
#include<iostream>
#include<new>
using namespace std; struct ListNode
{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL) {}
}; void createList(ListNode *&L)
{
int arr[]= {,,,,,,,,,};
int i;
ListNode *p=NULL;
for(i=; i<; i++)
{
ListNode *tmp=new ListNode(arr[i]);
if(L==NULL)
{
L=tmp;
p=tmp;
}
else
{
p->next=tmp;
p=tmp;
}
}
} void deleteNode(ListNode *c)
{
if(c==NULL||c->next==NULL)
return;
ListNode *d=c->next;
c->next=d->next;
c->val=d->val;
d->next=NULL;
delete(d);
} int main()
{
ListNode *head=NULL;
createList(head);
ListNode *p=head;
while(p)
{
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
deleteNode(head->next->next->next);
p=head;
while(p)
{
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
}
careercup-链表 2.3的更多相关文章
- [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项
2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...
- [CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素
2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元 ...
- [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 ...
- [CareerCup] 2.4 Partition List 划分链表
2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- [CareerCup] 2.7 Palindrome Linked List 回文链表
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
- [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表
4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each de ...
- [CareerCup] 17.13 BiNode 双向节点
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...
- 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST
引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...
- Careercup - Google面试题 - 5735304249999360
2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...
随机推荐
- HTML DOM Select 对象
Select 对象 Select 对象代表 HTML 表单中的一个下拉列表. 在 HTML 表单中,<select> 标签每出现一次,一个 Select 对象就会被创建. 您可通过遍历表单 ...
- CSS3 calc() 会计算的属性
calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能,你可以使用calc()给元素的border.margin.pading.font-size和width等属性设置动态值. ...
- python学习之”迭代从入门到精通“
在开发的过程中,假如给你一个list或者tuple,我们可以通过for循环来遍历这个list或者tuple,这种遍历我们成为迭代(Iteration).在Python中,迭代是通过for ... in ...
- uva 12206 - Stammering Aliens
基于hash的LCP算法: #include<cstdio> #include<cstring> #include<algorithm> #define maxn ...
- 合并 ios 静态库
合并 cordovaLib库: lipo -create 'Release-iphoneos/libCordova.a' "Release-iphonesimulator/libCordov ...
- 【HDOJ】2206 IP的计算
题目很简单,情况有很多种. #include <stdio.h> #include <string.h> ]; int isIPaddr(char buf[]) { int i ...
- Unreachable catch block for IOException. This exception is never thrown from the try statement body
Unreachable catch block for IOException. This exception is never thrown from the try statement body ...
- MySQL5.6 基于db的并行复制
slave的几个类结构: Master_info:用于IO线程的参数,包括连接master实例的信息. Relay_log_info:用于sql线程,表示relay log相关的信 ...
- 通过 WIN32 API 实现嵌入程序窗体
写了一个不使用 COM, 而是通过 WIN32 API 实现的示例, 它把写字板程序嵌在了自己的一个面板中. 这么做可能没有实际意义, 因为两个程序之前没有进行有价值的交互, 这里仅仅是为了演示这么做 ...
- Codevs_1017_乘积最大_(划分型动态规划/记忆化搜索)
描述 http://codevs.cn/problem/1017/ 给出一个n位数,在数字中间添加k个乘号,使得最终的乘积最大. 1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提 ...