1.逆序列印單鏈表(遞迴)
2.刪除乙個無頭結點的單鏈表的非尾節點(不能遍歷鍊錶)#include
#include
#include
typedef
char linktype;
typedef
struct linknodelinknode;
void linklistreverseprint(linknode* head)
if (head->next != null)
printf("%c ", head->data);
}
這裡運用的方法就是將要刪除的節點的下乙個節點的內容放到pos中,將pos->next 刪除,從而刪除了我們想要刪除的內容。void linklisterase(linknode** head, linknode* pos)
if (*head ==
null)
if (pos->next ==
null)
linknode* to_delete = pos->next;
pos->
data
= to_delete->
data;
pos->next = to_delete->next;
linklistdestroynode(to_delete);//銷毀節點,防止記憶體洩漏
return;
}
3.在無頭單鏈表的乙個節點前插入乙個節點(不能遍歷鍊錶)
4.單鏈表實現約瑟夫環(josephcircle)void linklistinsertbefore(linknode** head, linknode* pos, linktype value)
if (*head == null)
if (*head == pos)
linknode* cur = *head;
while (cur->next != pos && cur->next != null)
linknode* new_node = linklistcreatenode(value);
cur->next = new_node;
new_node->next = pos;
return;
}
5.單鏈表的氣泡排序linknode* josephcircle(linknode* head, size_t n)
linknode* cur = head;
while(cur->next != cur)
linknode* to_delete = cur->next;
cur->
data
= to_delete->
data;
cur->next = to_delete->next;
linklistdestroynode(to_delete);
}return cur;
}
6.合併兩個有序鍊錶,合併後依然有序void linklistbubblesort(linknode* head)
linknode* cur = head;
while (cur !=
null)
ptr = ptr->next;
}cur = cur->next;}}
7.查詢單鏈表的中間節點,要求只能遍歷一遍鍊錶linknode* linklistmerge(linknode* head1, linknode* head2)
if (head2 ==
null)
linknode* head =
null;
linknode* cur1 = head1;
linknode* cur2 = head2;
if (cur1->
data
> cur2->
data) else
if(cur1->
data
< cur2->
data) else
linknode* cur = head;
while (cur1 !=
null
&& cur2 !=
null) else
if(cur1->
data
< cur2->
data) else
cur = cur->next;
}if (cur1 !=
null)
if (cur2 !=
null)
return head;
}
利用快慢指標,快指標每次走一步,慢指標每次走兩步,當快指標指向了鍊錶末尾時,慢指標正好指向了鍊錶的中間節點。linknode* findmidnode(linknode* head)
linknode* fast = head;
linknode* slow = head;
while (fast != null && fast->next != null)
return slow;
}
8.查詢單鏈表的倒數第k個節點,要求只能遍歷一次鍊錶
本題的思路與上一題的相似,先讓乙個指標走k步,再讓另乙個指標出發,兩個指標的速度都是每次走一步,這樣到先走的指標到達鍊錶末尾時,後走的指標正好走到了倒數第k個節點處。linknode* findlastknode(linknode* head, size_t k)
size_t size = linklistsize(head);
if (k == null || k > size)
linknode* fast = head;
linknode* slow = head;
while (--k)
while (fast != null && fast->next != null)
return slow;
}
9.刪除鍊錶的倒數第k個節點
10.判斷鍊錶是否帶環void eraselastknode(linknode** head, size_t k)
if (*head ==
null)
if (k ==
1) else
}
快慢指標,快指標每次走兩步,慢指標每次走一步,如果兩個指標相遇,說明鍊錶一定帶環。int hascycle(linknode* head)
}return
0;}
11. 單鏈表的逆置
12.若帶環,求環的長度;求環的入口linknode* linklistreverse(linknode** head)
if (*head == null || (*head)->next == null)
linknode* ptr = null;
linknode* cur = *head;
while (cur != null)
return ptr;
}
求環的長度:定義乙個指標,從相遇點的下乙個節點出發,經過一圈回到相遇點的時候,指標正好走過了一圈,只需要定義乙個計數器,得到指標走過的步數即可。linknode* hascycle(linknode* head)
}return0;}
size_t getcyclelen(linknode* head)
return len;}}
return
null;
}linknode* getcycleentry(linknode* head)
while (cur != ret)
return ret;
}
求環的入口點如下圖:
面試收集 關於鍊錶的一些面試題
鍊錶相關的面試題是經常出現的,今天總結一下 1.如何判斷乙個鍊錶是否有環?如果有,找到環的入口?設定快慢指標,快指標步長為2,慢指標步長為1,如果有環,最終快慢指標會相遇,如下 bool hascircle node head,node encounter fast null fast next n...
關於鍊錶的筆試 面試題
以下題目 於leetcode 有兩個鍊錶,它們表示逆序的兩個非負數。計算出兩個數的和之後,同樣逆序輸出作為乙個鍊錶 比如 2 1 3 5 6 4 得到 7 0 8 add two numbers class solution if carry 0 prev next new listnode car...
一些典型的筆試面試題
關於c c 基礎知識的面試題 已知string類定義如下,嘗試寫出類的成員函式實現。class string 答 string string const char str else string string const string another string string operator c...