【C语言】单链表的相关热点面试题(包括:从尾到头打印,逆置,冒泡,寻找中间节点,倒数k节点)

https://blog.csdn.net/hanjing_1995/article/details/51539599

  1. 从尾到头打印单链表

[cpp]  view plain  copy
  1. void FromTailToHeadPrint(SListNode*& head)  
  2. {  
  3.     stack<SListNode*> s;  
  4.     SListNode* cur = head;  
  5.     while (cur)  
  6.     {  
  7.         s.push(cur);  
  8.         cur = cur->_next;  
  9.     }  
  10.   
  11.     while (!s.empty())  
  12.     {  
  13.         cout << s.top()->_data <<"->";  
  14.         s.pop();  
  15.     }  
  16.     cout << ""<<endl;  
  17. }  



2.除一个无头单链表的非尾节点

[cpp]  view plain  copy
  1. void Erase(SListNode*& head,SListNode* pos)  
  2. {  
  3.     //分情况:空节点、一个节点、两个节点、三个节点  
  4.     if (head == NULL || pos==NULL)  
  5.     {  
  6.         return;  
  7.     }  
  8.       
  9.     if (head == pos)  
  10.     {  
  11.         free(head);  
  12.         head = NULL;  
  13.         return;  
  14.     }  
  15.   
  16.     SListNode* cur = head;  
  17.     while (cur)  
  18.     {  
  19.         SListNode* next = cur->_next;  
  20.           
  21.         if (next == pos)  
  22.         {  
  23.             //若只有两个节点,pos=next,nextnext=NULL,cur->_next = nextnext;  
  24.             //(即使题设未说明删除非尾节点)依然可以删除成功。  
  25.             SListNode* nextnext = next->_next;  
  26.             cur->_next = nextnext;  
  27.             free(next);  
  28.             next = NULL;  
  29.         }  
  30.         cur = cur->_next;  
  31.     }  
  32. }  



3.逆置、反转单链表

[cpp]  view plain  copy
  1. void Reverse(SListNode*& head)  
  2. {  
  3.     if (head == NULL)  
  4.     {  
  5.         return;  
  6.     }  
  7.     SListNode* cur = head;  
  8.     SListNode* next = NULL;  
  9.     while (cur)  
  10.     {  
  11.         SListNode* tmp = cur;  
  12.         cur = cur->_next;  
  13.         tmp->_next = next;  
  14.         next = tmp;  
  15.         head = tmp;  
  16.     }  
  17. }  



4.单链表冒泡排序

[cpp]  view plain  copy
  1. void BubbleSort(SListNode*& head)  
  2. {  
  3.     //空节点或一个节点直接返回  
  4.     if (head == NULL || head->_next == NULL)  
  5.     {  
  6.         return;  
  7.     }  
  8.       
  9.     SListNode* begin = head;  
  10.     SListNode* cur = head;  
  11.   
  12.     while (begin->_next)  
  13.     {          
  14.         while (cur->_next)  
  15.         {  
  16.             if (cur->_data > cur->_next->_data)  
  17.             {  
  18.                 swap(cur->_data, cur->_next->_data);  
  19.             }  
  20.             cur = cur->_next;  
  21.         }  
  22.         begin = begin->_next;  
  23.     }  
  24. }  



5.查找单链表的中间节点,要求只能遍历一次链表

[cpp]  view plain  copy
  1. SListNode* FindMiddle(SListNode*& head)  
  2. {  
  3.     if (head == NULL)  
  4.     {  
  5.         return NULL;  
  6.     }  
  7.     SListNode* slow = head;  
  8.     SListNode* fast = head;  
  9.   
  10.     while (fast->_next)  
  11.     {  
  12.         if (fast->_next)  
  13.         {  
  14.             fast = fast->_next;  
  15.             if (fast->_next)  
  16.             {  
  17.                 fast = fast->_next;  
  18.             }  
  19.             else  
  20.             {  
  21.                 return slow;  
  22.             }  
  23.             slow = slow->_next;  
  24.         }  
  25.         else  
  26.         {  
  27.             return NULL;  
  28.         }  
  29.     }  
  30.       
  31.     return slow;  
  32. }  



6.查找单链表的倒数第k个节点,要求只能遍历一次链表

[cpp]  view plain  copy
  1. SListNode* FindLastK(SListNode*& head,int k)  
  2. {  
  3.     assert(k > 0);  
  4.     if (head == NULL)  
  5.     {  
  6.         return NULL;  
  7.     }  
  8.     SListNode* slow = head;  
  9.     SListNode* fast = head;  
  10.     while (k--)  
  11.     {  
  12.         if (fast->_next)  
  13.         {  
  14.             fast = fast->_next;  
  15.         }  
  16.         else  
  17.         {  
  18.             return NULL;  
  19.         }  
  20.     }  
  21.     slow = slow->_next;  
  22.     while (fast->_next)  
  23.     {  
  24.         fast = fast->_next;  
  25.         slow = slow->_next;  
  26.     }  
  27.     return slow;  
  28. }  


本文出自 “Han Jing's Blog” 博客,请务必保留此出处http://10740184.blog.51cto.com/10730184/1772313



猜你喜欢

转载自blog.csdn.net/sinat_35297665/article/details/80567971