P99、面试题13:在o(1)时间删除链表结点
题目:给定单向链表的头指针和一个结点指针,定义一个函数在o(1)时间删除该结点。链表结点与函数的定义如下:
struct ListNode{
int m_nValue;
ListNode *m_pNext;
}
void DeleteNode(Listnode** pListHead, ListNode* pToBeDeleted);
package com.yyq; /**
* Created by Administrator on 2015/9/13.
*/
public class ListNode {
private int m_nValue;
private ListNode m_pNext; public ListNode(int m_nValue){
this.m_nValue = m_nValue;
} public ListNode getM_pNext() {
return m_pNext;
} public void setM_pNext(ListNode m_pNext) {
this.m_pNext = m_pNext;
} public int getM_nValue() {
return m_nValue;
} public void setM_nValue(int m_nValue) {
this.m_nValue = m_nValue;
}
}
实现类:
package com.yyq; /**
* Created by Administrator on 2015/9/13.
*/
public class DeleteNodeInList {
public static ListNode deleteNode(ListNode pListHead, ListNode pToBeDeleted){
if (null == pListHead || null == pToBeDeleted) {
return pListHead;
}
//要删除的结点不是尾结点
if (pToBeDeleted.getM_pNext()!= null){
ListNode pNext = pToBeDeleted.getM_pNext();
pToBeDeleted.setM_nValue(pNext.getM_nValue());
pToBeDeleted.setM_pNext(pNext.getM_pNext());
pNext = null;
}
//链表只有一个结点,删除头结点(也是尾结点)
else if(pListHead == pToBeDeleted){
pToBeDeleted = null;
pListHead = null;
}
//链表中有多个结点,删除尾结点
else{
ListNode pNode = pListHead;
while(pNode.getM_pNext() != pToBeDeleted){
pNode = pNode.getM_pNext();
}
pNode.setM_pNext(null);
pToBeDeleted = null;
}
return pListHead;
} public static void printList(ListNode pListHead){
if (pListHead == null)
return;
ListNode pNode = pListHead;
while(pNode!= null){
System.out.print(pNode.getM_nValue()+" ");
pNode = pNode.getM_pNext();
}
System.out.println();
}
// ====================测试代码====================
public static void Test(ListNode pListHead, ListNode pNode)
{
System.out.println("The original list is: ");
printList(pListHead);
if (null == pListHead || null == pNode) {
return ;
}
System.out.println("The node to be deleted is: " + pNode.getM_nValue());
pListHead = deleteNode(pListHead, pNode);
System.out.println("The result list is: ");
printList(pListHead);
System.out.println();
} // 链表中有多个结点,删除中间的结点
public static void Test1()
{
ListNode pNode1 = new ListNode(1);
ListNode pNode2 = new ListNode(2);
ListNode pNode3 = new ListNode(3);
ListNode pNode4 = new ListNode(4);
ListNode pNode5 = new ListNode(5);
ListNode pNode6 = new ListNode(6); pNode1.setM_pNext(pNode2);
pNode2.setM_pNext(pNode3);
pNode3.setM_pNext(pNode4);
pNode4.setM_pNext(pNode5);
pNode5.setM_pNext(pNode6); Test(pNode1, pNode3);
pNode1 = null;
} // 链表中有多个结点,删除尾结点
public static void Test2()
{
ListNode pNode1 = new ListNode(1);
ListNode pNode2 = new ListNode(2);
ListNode pNode3 = new ListNode(3);
ListNode pNode4 = new ListNode(4);
ListNode pNode5 = new ListNode(5);
ListNode pNode6 = new ListNode(6); pNode1.setM_pNext(pNode2);
pNode2.setM_pNext(pNode3);
pNode3.setM_pNext(pNode4);
pNode4.setM_pNext(pNode5);
pNode5.setM_pNext(pNode6); Test(pNode1, pNode6);
pNode1 = null;
} // 链表中有多个结点,删除头结点
public static void Test3()
{
ListNode pNode1 = new ListNode(1);
ListNode pNode2 = new ListNode(2);
ListNode pNode3 = new ListNode(3);
ListNode pNode4 = new ListNode(4);
ListNode pNode5 = new ListNode(5);
ListNode pNode6 = new ListNode(6); pNode1.setM_pNext(pNode2);
pNode2.setM_pNext(pNode3);
pNode3.setM_pNext(pNode4);
pNode4.setM_pNext(pNode5);
pNode5.setM_pNext(pNode6); Test(pNode1, pNode1);
pNode1 = null;
} // 链表中只有一个结点,删除头结点
public static void Test4()
{
ListNode pNode1 = new ListNode(1); Test(pNode1, pNode1);
} // 链表为空
public static void Test5()
{
Test(null, null);
} public static void main(String[] args){
Test1();
Test2();
Test3();
Test4();
Test5();
}
}
P99、面试题13:在o(1)时间删除链表结点的更多相关文章
- 剑指Offer:面试题13——在O(1)时间删除链表结点
问题描述: 给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点.链表结点与函数的定义如下: public class ListNode{ int value; ListNode ...
- 面试题:在O(1)时间删除链表结点
题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点. 链表结点与函数的定义如下: struct ListNode { int m_nValue; ListNode* m_p ...
- 剑指offer-面试题13.在O(1)时间删除链表节点
题目:给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间删除该节点. 链表节点与函数的定义如下. 通常我们删除某个节点都是从头开始遍历到需要删除节点的前一个节点. 然后使得该节点的next ...
- 面试题13:在O(1)时间删除链表节点
http://blog.csdn.net/jsqfengbao/article/details/47175249
- 《剑指offer》面试题13 在O(1)时间删除链表节点 Java版
这道题的关键是知道找到尾节点的前一个节点必须遍历,而且这样做了之后总的时间复杂度还是O(1),以及如何不破坏链表删除一个已知节点 public ListNode delete(ListNode hea ...
- (剑指Offer)面试题13:在O(1)时间内删除链表结点
题目: 在给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间内删除该结点.链表结点与函数的定义如下: struct ListNode{ int val; ListNode* next; } ...
- 剑指offer编程题Java实现——面试题13在O(1)时间内删除链表节点
题目:给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间删除该节点. 由于给定的是单向链表,正常删除链表的时间复杂度是查找链表的时间复杂度即O(n),如果要求在O(1)时间复杂度内删除节点 ...
- 【面试题013】在O(1)时间删除链表结点
[面试题013]在O(1)时间删除链表结点 我们要删除结点i,我们可以把结点i的下一个结点j的内容复制到结点i,然后呢把结点i的指针指向结点j的下一个结点.然后在删除结点j. 1.如果结点i位于链表 ...
- 面试题18(一):在O(1)时间删除链表结点
// 面试题18(一):在O(1)时间删除链表结点 // 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该 // 结点.链表结点与函数的定义如下: // struct Lis ...
- 题目13 在O(1)时间删除链表节点
///////////////////////////////////////////////////////////////////////////////////// // 3. 题目13 在O( ...
随机推荐
- C#获取“所有用户桌面”的路径
想用C#得到The All Users Desktop(Public\Desktop)的路径. 原来以为很简单,然而 Environment.GetFolderPath(Environment.Spe ...
- Clone table header and set as the first element, and replace header's th with td
Clone table header and replace header's th with td var tableHeaderRow = '#tableId tbody tr:nth-child ...
- canvas 的学习
canvas 绘制直线的API有: 1.moveTo()起点坐标. 2.lineTo()绘制的直线 3. fillStyle以及 flii()是绘制实体的 4. strokeStyle 和stroke ...
- Delphi中的四舍五入函数
一.Delphi中的四舍五入法 四舍五入是一种应用非常广泛的近似计算方法,针对不同的应用需求,其有算术舍入法和银行家舍入法两种. 所谓算术舍入法,就是我们通常意义上的四舍五入法.其规则 ...
- numpy简单入门
声明:本文大量参考https://www.dataquest.io/mission/6/getting-started-with-numpy(建议阅读原文) 读取文件 有一个名为world_alc ...
- DB2中循环日期跑数据
1.数据库版本: 2.具体实现方式: ),)) /*************************************************************************** ...
- 升级python版本导致Django无法使用的解决办法
运行环境是CentOS6.2 x86_64,在把python从2.6.6升级到2.7.5后,由于环境变量的改变,在python代码中再import django的话将会出现以下报错: “No mo ...
- XAML系列学习
在XAML中为属性赋值 1.使用Attribute=value形式 <Rectangle Width="100" Height="100" Stroke= ...
- 操作数据(insert、update、delete)
插入数据 使用Insert Into 插入 if(exists(select * from sys.databases where name = 'webDB')) drop database web ...
- Object c中的alloc和init问题
从开始学的NSString *name=[[NSString alloc] init] 起,老师教这句话是分配内存空间,一直在用,从来没考虑过它的内部是怎么实现的.今天无意中看到了这一句代码 NSSt ...