链表笔试面试题

http://blog.csdn.net/fatshaw/article/details/6452460

1.已知链表的头结点head,写一个函数把这个链表逆序

[cpp] 
view plain
copy

  1. void List::reverse()  
  2. {  
  3.         list_node * p = head;  
  4.         list_node * q = p->next;  
  5.         list_node * r = NULL;  
  6.         while(q){  
  7.                 r = q->next;  
  8.                 q->next = p;  
  9.                 p = q;  
  10.                 q = r;  
  11.         }  
  12.         head->next  = NULL;  
  13.         head = p;  
  14. }  

递归方法:

[cpp] 
view plain
copy

  1. void List::reverse2(list_node * curnode)  
  2. {  
  3.         if(curnode ==NULL)curnode = head;  
  4.         if(curnode->next==NULL)  
  5.         {  
  6.                 cur = curnode;  
  7.                 return;  
  8.         }  
  9.   
  10.         reverse2(curnode->next);  
  11.         curnode->next->next=curnode;  
  12.         if(curnode == head)  
  13.         {  
  14.                 head=cur;  
  15.                 cur = curnode;  
  16.                 cur->next = NULL;  
  17.         }  
  18. }  

2.已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。

[cpp] 
view plain
copy

  1. void List::merge(List & list)  
  2. {         
  3.         list_node * a = head;  
  4.         list_node * b = list.head;  
  5.         list_node * tempa= a;  
  6.         list_node * tempb = b;  
  7.         while(a&&b)   
  8.         {  
  9.                 if(a->value <= b->value)  
  10.                 {  
  11.                         while(a&&b&&a->value <= b->value)  
  12.                         {  
  13.                                 tempa = a;  
  14.                                 a = a->next;  
  15.                         }  
  16.                         tempa->next=b;  
  17.                 }  
  18.                 else  
  19.                 {  
  20.                         while(a&&b&&a->value > b->value)  
  21.                         {  
  22.                                 tempb = b;  
  23.                                 b = b->next;  
  24.                         }  
  25.                         tempb->next=a;  
  26.                 }  
  27.   
  28.         }  
  29. }  

递归方法:


[cpp] 
view plain
copy

  1. list_node* List::recursive_merge(list_node * a,list_node * b)  
  2. {  
  3.         if(a == NULL)return b;  
  4.         if(b == NULL)return a;  
  5.         if(a->value <= b->value){  
  6.                 a->next=recursive_merge(a->next,b);  
  7.                 return a;  
  8.         }  
  9.         if(a->value > b->value)  
  10.         {  
  11.                 b->next=recursive_merge(a,b->next);  
  12.                 return b;  
  13.         }  

3.有一个链表L,其每个节点有2个指针,一个指针next指向链表的下个节点,另一个random随机指向链表中的任一个节点,可能是自己或者为空,写一个程序,要求复制这个链表的结构并分析其复杂性

这个题目的方法很巧妙,将两个链表连接起来形成一个链表,设置好random指针后再将两个链表分割开来。

 《链表笔试面试题》

[cpp] 
view plain
copy

  1. List List::copyRndList()  
  2. {  
  3.         list_node * newhead = NULL;  
  4.         list_node * newcur = NULL;  
  5.         list_node * cur = head;  
  6.         while(cur)  
  7.         {  
  8.                 if(newhead == NULL){  
  9.                         newhead = new list_node;  
  10.                         newhead->value = cur->value;  
  11.                         newhead->next = cur->next;  
  12.                         cur->next = newhead;  
  13.                 }  
  14.                 else{  
  15.                         newcur = new list_node;  
  16.                         newcur->value = cur->value;  
  17.                         newcur->next = cur->next;  
  18.                         cur->next = newcur;  
  19.                 }  
  20.                 cur=cur->next->next;  
  21.         }  
  22.         cur = head;  
  23.         while(cur){  
  24.                 if(cur->rnd)  
  25.                         cur->next->rnd=cur->rnd->next;  
  26.                 else  
  27.                         cur->next->rnd = NULL;  
  28.                 cur = cur->next->next;  
  29.         }  
  30.         cur = head;  
  31.         list_node * dst = cur->next;  
  32.         while(cur)  
  33.         {  
  34.                 list_node * temp = dst->next;  
  35.                 cur->next = temp;  
  36.                 if(temp)dst->next = temp->next;  
  37.                 cur = cur->next;  
  38.                 dst=dst->next;  
  39.         }  
  40.         List newList;  
  41.         newList.head = newhead;  
  42.         return newList;  
  43. }  

4. 找出单向链表中中间结点

两个指针,一个步长为1,另一个步长为2.步长为2的走到底后步长为1的正好到中间。

[cpp] 
view plain
copy

  1. list_node * List::middleElement()  
  2. {  
  3.         list_node * p = head;  
  4.         list_node * q = head->next;  
  5.         while(q){  
  6.                 p = p->next;  
  7.                 if(q)q=q->next;  
  8.                 if(q)q=q->next;  
  9.         }  
  10. }  

5. 如何检查一个单向链表上是否有环

同样两个指针,一个步长为1,另一个步长为2,如果两个指针能相遇则有环。

[cpp] 
view plain
copy

  1. list_node * List::getJoinPointer()  
  2. {  
  3.   
  4.         if(head == NULL || head->next == NULL)return NULL;  
  5.         list_node * one = head;  
  6.         list_node * two = head->next;  
  7.         while(one != two){  
  8.                 one = one->next;  
  9.                 if(two)two=two->next;  
  10.                 else break;  
  11.                 if(two)two=two->next;  
  12.                 else break;  
  13.         }  
  14.         if(one == NULL || two == NULL)return NULL;  
  15.         return one;  
  16. }  

6. 给定单链表(head),如果有环的话请返回从头结点进入环的第一个节点。

设链表头到环入口节点距离为x,环入口节点到两个指针相遇节点距离为z,换长度为y,则有x+z+1=y,所以z=y-1-x,即一个指针从链表头部开始移动,一个指针两个指针相遇后一个节点开始移动,相遇的地方即为环入口

[cpp] 
view plain
copy

  1. list_node * List::findCycleEntry()  
  2. {  
  3.         if(checkCycle()==false)return NULL;  
  4.         list_node * joinPointer = getJoinPointer();  
  5.         list_node * p = head;  
  6.         list_node * q = joinPointer->next;  
  7.         while(p!=q)  
  8.         {  
  9.                 p=p->next;  
  10.                 q=q->next;  
  11.         }  
  12.         return p;  
  13. }  

7.只给定单链表中某个结点p(并非最后一个结点,即p->next!=NULL)指针,删除该结点。

将p后面那个节点的值复制到p,删除p后面的节点

[cpp] 
view plain
copy

  1. void List::deleteByPointer(list_node * node)  
  2. {  
  3.         if(node)  
  4.         {  
  5.                 if(node->next){  
  6.                         node->value = node->next->value;  
  7.                         node->next = node->next->next;  
  8.                 }  
  9.         }  
  10. }  

8.在p前面插入一个节点

在p后面插入新节点,将p的值与新建的节点值互换。

 

9.给定单链表头结点,删除链表中倒数第k个结点

一个指针指向链表头,另一个指针指向第k个指针,然后两个指针一起移动,第二个指针到了末端则第一个指针就是倒数第k个节点

[cpp] 
view plain
copy

  1. list_node * List::lastKelement(int k){  
  2.         int t = k;  
  3.         list_node * p = head;  
  4.         while(p&&t){  
  5.                 p=p->next;  
  6.                 t–;  
  7.         }  
  8.         if(p == NULL && t >0)return NULL;  
  9.         list_node * q=head;  
  10.         while(q && p){  
  11.                 p=p->next;  
  12.                 q=q->next;  
  13.         }  
  14.         return q;  
  15. }  

 

10. 判断两个链表是否相交。

两种情况,如果链表有环,则先在环里设定一个指针不动,另一个链表从头开始移动,如果另一个链表能够与环中的指针相遇则是相交。

如果没有环,则判断两个链表的最后个节点是否相同,相同则相交

[cpp] 
view plain
copy

  1. bool List::isIntersecting(const List & list)  
  2. {  
  3.         bool flag = false;  
  4.         if(this->checkCycle())  
  5.         {  
  6.                 list_node * p = getJoinPointer();  
  7.                 list_node * q = list.head;  
  8.                 while(q){  
  9.                         if(q == p){  
  10.                                 flag = true;  
  11.                                 break;  
  12.                         }  
  13.                         q=q->next;  
  14.                 }  
  15.                 flag = true;  
  16.         }  
  17.         else  
  18.         {  
  19.                 list_node * p = head;  
  20.                 list_node * q = list.head;  
  21.                 while(p->next)p=p->next;  
  22.                 while(q->next)q=q->next;  
  23.                 if(p  == q)flag = true;  
  24.                 else flag =false;  
  25.         }  
  26.         return flag;  
  27. }  

 

11. 两个链表相交,找出交点

求出两个链表的长度a和b,一个指针指向较短链表的头head,另一个指针指向较长链表的第head+|a-b|,然后两个指针一起移动,相遇处即为交点。

[cpp] 
view plain
copy

  1. list_node * List::intersectNode(const List & list)  
  2. {  
  3.         if(!isIntersecting(list))return NULL;  
  4.         int a = cnt;  
  5.         int b = list.cnt;  
  6.         list_node * p;  
  7.         list_node * q;  
  8.         if(a<b){p=list.head;q = head;}  
  9.         else {p = head; q=list.head;}  
  10.         a = abs(cnt – list.cnt);  
  11.         while(p && a)  
  12.         {  
  13.                 p = p->next;  
  14.                 a–;  
  15.         }  
  16.         while(p&&q)  
  17.         {  
  18.                 if(q==p)break;  
  19.                 p=p->next;  
  20.                 q=q->next;  
  21.         }  
  22.         if(p && q && p == q)return p;  
  23.         return NULL;  
  24. }  
    原文作者:lonelywinter340
    原文地址: https://blog.csdn.net/lonelywinter340/article/details/14053363
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞