题目:给定单向链表的头指针和一个结点指针,定义一个函数在o(1)时间删除该结点。链表结点与函数的定义如下:
struct ListNode{
       int m_nValue;
       ListNode *m_pNext;    
}
void DeleteNode(Listnode** pListHead, ListNode* pToBeDeleted);

思路:将要删除的结点的下一个结点的值复制到要删除的结点,删除下一个结点。如果是要删除最后一个结点,则要从头开始搜索。
 
测试用例:
1)功能测试(从有多个结点的链表的中间删除一个结点,从有多个结点的链表中删除头结点,从有多个结点的链表中删除尾结点,从只有一个结点的链表中删除唯一的结点)
2)特殊输入测试(指向链表头结点指针的为null指针,指向要删除的结点为null指针)
 
代码实现:
结点类:
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();
}
}
输出结果:
The original list is: 
1  2  3  4  5  6  
The node to be deleted is: 3
The result list is: 
1  2  4  5  6  
 
The original list is: 
1  2  3  4  5  6  
The node to be deleted is: 6
The result list is: 
1  2  3  4  5  
 
The original list is: 
1  2  3  4  5  6  
The node to be deleted is: 1
The result list is: 
2  3  4  5  6  
 
The original list is: 
1  
The node to be deleted is: 1
The result list is: 
 
The original list is: 
 

P99、面试题13:在o(1)时间删除链表结点的更多相关文章

  1. 剑指Offer:面试题13——在O(1)时间删除链表结点

    问题描述: 给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点.链表结点与函数的定义如下: public class ListNode{ int value; ListNode ...

  2. 面试题:在O(1)时间删除链表结点

    题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点. 链表结点与函数的定义如下: struct ListNode { int m_nValue; ListNode* m_p ...

  3. 剑指offer-面试题13.在O(1)时间删除链表节点

    题目:给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间删除该节点. 链表节点与函数的定义如下. 通常我们删除某个节点都是从头开始遍历到需要删除节点的前一个节点. 然后使得该节点的next ...

  4. 面试题13:在O(1)时间删除链表节点

    http://blog.csdn.net/jsqfengbao/article/details/47175249

  5. 《剑指offer》面试题13 在O(1)时间删除链表节点 Java版

    这道题的关键是知道找到尾节点的前一个节点必须遍历,而且这样做了之后总的时间复杂度还是O(1),以及如何不破坏链表删除一个已知节点 public ListNode delete(ListNode hea ...

  6. (剑指Offer)面试题13:在O(1)时间内删除链表结点

    题目: 在给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间内删除该结点.链表结点与函数的定义如下: struct ListNode{ int val; ListNode* next; } ...

  7. 剑指offer编程题Java实现——面试题13在O(1)时间内删除链表节点

    题目:给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间删除该节点. 由于给定的是单向链表,正常删除链表的时间复杂度是查找链表的时间复杂度即O(n),如果要求在O(1)时间复杂度内删除节点 ...

  8. 【面试题013】在O(1)时间删除链表结点

    [面试题013]在O(1)时间删除链表结点  我们要删除结点i,我们可以把结点i的下一个结点j的内容复制到结点i,然后呢把结点i的指针指向结点j的下一个结点.然后在删除结点j. 1.如果结点i位于链表 ...

  9. 面试题18(一):在O(1)时间删除链表结点

    // 面试题18(一):在O(1)时间删除链表结点 // 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该 // 结点.链表结点与函数的定义如下: // struct Lis ...

  10. 题目13 在O(1)时间删除链表节点

    ///////////////////////////////////////////////////////////////////////////////////// // 3. 题目13 在O( ...

随机推荐

  1. [java学习笔记]java语言基础概述之转义字符&break&continue

    1.转义字符 \t:制表符 \n:回车 \b:退格 \r:回车 \":双引号 \\:反斜线(常用于文件路径的书写中)   windows系统中回车符其实是由两个符号组成的,\r\n linu ...

  2. checkBox控件的CheckedChanged与CheckedStateChanged区别

    Checked属性为bool类型,CheckState属性为枚举类型(CheckState.Checked.CheckState.Unchecked和CheckState.Indeterminate) ...

  3. NodeJS + Socket.io搭建聊天服务器

    第一步:安装node git clone https://github.com/joyent/node.git cd node git checkout v0.10.33-release ./conf ...

  4. XE5 ANDROID平台 调用 webservice

    服务端需要midas.dll   XE5对android的平台支持很有吸引力,虽然目前用来直接开发应用到安卓市场卖赚钱可能性估计不大(安卓市场目前国内好像都是免费的天下),但是对于企业应用很是很有帮助 ...

  5. table 中实现 控制 指定列的 左对齐 右对齐方式

    .listTable{ border-collapse:collapse; border-top:1px solid #8c9594; border-right:1px solid #8c9594; ...

  6. mySQL时间

    " and day='".date('Y-m-d',strtotime($day_g)). "'";  时间如: 2014-09-09 and day>= ...

  7. Unix/Linux中shell调用sqlplus的方式

    Unix/Linux下,shell脚本调用sqlplus的几种方式介绍: 一.最简单的shell调用sqlplus #!/bin/bash sqlplus -S /nolog > sqlplus ...

  8. Nginx upstream的5种权重分配方式

    .轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,后端服务器down掉,能自动剔除 .weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况. upstre ...

  9. Create a SharePoint Application Page for Anonymous Access

    http://dishasharepointworld.blogspot.com/2011/07/how-to-create-sharepoint-application_1072.html http ...

  10. 超简单的卸载ORACLE 11g

    本机环境 win10 64位 找到安装目录下的 F:\app\Shuai\product\11.2.0\dbhome_1 按键盘d找到deinstall文件夹进入 管理员运行deinstall.bat ...