鏈表 面試題整理

轉自http://blog.csdn.net/fatshaw/article/details/6452460?reload

看完覺得獲益匪淺啊,分享過來一下~~

 

1.已知鏈表的頭結點head,寫一個函數把這個鏈表逆序

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

遞歸方法:

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

2.已知兩個鏈表head1 和head2 各自有序,請把它們合併成一個鏈表依然有序。

  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.         } 

遞歸方法:

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

3.有一個鏈表L,其每個節點有2個指針,一個指針next指向鏈表的下個節點,另一個random隨機指向鏈表中的任一個節點,可能是自己或者爲空,寫一個程序,要求複製這個鏈表的結構並分析其複雜性

這個題目的方法很巧妙,將兩個鏈表連接起來形成一個鏈表,設置好random指針後再將兩個鏈表分割開來。

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

4. 找出單向鏈表中中間結點

兩個指針,一個步長爲1,另一個步長爲2.步長爲2的走到底後步長爲1的正好到中間。

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

5. 如何檢查一個單向鏈表上是否有環

同樣兩個指針,一個步長爲1,另一個步長爲2,如果兩個指針能相遇則有環。

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

6. 給定單鏈表(head),如果有環的話請返回從頭結點進入環的第一個節點。

設鏈表頭到環入口節點距離爲x,環入口節點到兩個指針相遇節點距離爲z,換長度爲y,則有x+z+1=y,所以z=y-1-x,即一個指針從鏈表頭部開始移動,一個指針兩個指針相遇後一個節點開始移動,相遇的地方即爲環入口

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

7.只給定單鏈表中某個結點p(並非最後一個結點,即p->next!=NULL)指針,刪除該結點。

將p後面那個節點的值複製到p,刪除p後面的節點

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

8.在p前面插入一個節點

在p後面插入新節點,將p的值與新建的節點值互換。

9.給定單鏈表頭結點,刪除鏈表中倒數第k個結點

一個指針指向鏈表頭,另一個指針指向第k個指針,然後兩個指針一起移動,第二個指針到了末端則第一個指針就是倒數第k個節點

  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; 

10. 判斷兩個鏈表是否相交。

兩種情況,如果鏈表有環,則先在環裏設定一個指針不動,另一個鏈表從頭開始移動,如果另一個鏈表能夠與環中的指針相遇則是相交。

如果沒有環,則判斷兩個鏈表的最後個節點是否相同,相同則相交

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

11. 兩個鏈表相交,找出交點

求出兩個鏈表的長度a和b,一個指針指向較短鏈表的頭head,另一個指針指向較長鏈表的第head+|a-b|,然後兩個指針一起移動,相遇處即爲交點。

  1. list_node * List::intersectNode(const List & list) 
  2.         if(!isIntersecting(list))return NULL; 
  3.         int a = cnt; 
  4.         int b = list.cnt; 
  5.         list_node * p; 
  6.         list_node * q; 
  7.         if(a<b){p=list.head;q = head;} 
  8.         else {p = head; q=list.head;} 
  9.         a = abs(cnt - list.cnt); 
  10.         while(p && a) 
  11.         { 
  12.                 p = p->next; 
  13.                 a--; 
  14.         } 
  15.         while(p&&q) 
  16.         { 
  17.                 if(q==p)break
  18.                 p=p->next; 
  19.                 q=q->next; 
  20.         } 
  21.         if(p && q && p == q)return p; 
  22.         return NULL; 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章