找出单向链表的倒数第m个元素

链表节点:
class  Node
{
public:
   
int        data;
    Node
*    next;

public:
    Node(
int n) : data(n), next(0)
   
{
    }


    Node() : data(
0), next(0)
   
{
    }


    Node
* InsertAfter(int _data )
   
{
        Node
* newnode= new Node(_data);

        newnode
->next= next;
        next
= newnode;

       
return newnode;
    }

}
;

低效的查找算法:

Node* FindMToLastNode(Node* pHead,int m)
{
   
// 第一次遍历,获取链表的计数
    Node* pCurrent= pHead;
   
int nCount = 0;
   
while (pCurrent)
   
{
       
++nCount;
        pCurrent
= pCurrent->next;
    }


   
// 防止越界
   if (m > nCount) return0;

   
// 第二遍遍历,获取倒数第m个节点,也就是顺数滴n个节点
   int n = nCount - m+ 1;
    nCount
= 0;
    pCurrent
= pHead;
   
while (pCurrent)
   
{
       
++nCount;
       
if (nCount == n)
       
{
           
return pCurrent;
        }


        pCurrent
= pCurrent->next;
    }


   
return 0;
}

 


这个算法很低效,要循环列表两次,从时间上来说,消耗很大.从空间上,需要3个临时变量,消耗也有点大.


高效的查找算法:
 
 
[cpp]  view plain copy
  1. Node* FindMToLastNode(Node* pHead, int m)  
  2. {  
  3.         // 查找到第m个元素  
  4.     Node* pCurrent = pHead;  
  5.     for (int i = 0; i < m; ++i)  
  6.     {  
  7.         if (pCurrent)  
  8.         {  
  9.             pCurrent = pCurrent->next;  
  10.         }  
  11.         else  
  12.         {  
  13.             return NULL;  
  14.         }  
  15.     }  
  16.     // 一起继续遍历到链表尾部,  
  17.     // 现在pFind和pCurrent之间间隔了m个元素,  
  18.     // 所以,当pCurrent遍历到尾部的时候,  
  19.     // pFind就到了倒数第m个元素的位置上.  
  20.       
  21.     Node* pFind = pHead;  
  22.     while (pCurrent)  
  23.     {  
  24.         pFind        = pFind->next;  
  25.         // 间隔m个元素  
  26.         pCurrent    = pCurrent->next;  
  27.     }  
  28.   
  29.     return pFind;  
  30.  }  

 
这个算法果然精妙!空间上只需要开销两个临时变量,时间上只需要循环链表一遍,也就是O(n)!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值