鏈表面試題

2021-08-30 17:40:21 字數 1245 閱讀 4798

#pragma once

#include

typedef int datetype;

typedef struct listnode

listnode;

//單鏈表實現約瑟夫環

listnode * josephcycle(listnode *first, int k)

tail->next = first;

listnode *cur = first;

listnode *prve = null;

while (cur->next != cur)

prve->next = cur->next;

free(cur);

cur = prve->next;

}cur->next = null;

return cur;

}//合併兩個有序鍊錶,合併後依然有序

listnode * mergeorderedlist(listnode *list1, listnode *list2)

else                                  

}else

else

}if (cur1 == null)

if (cur2 == null)

return result;}}

//列印兩個有序鍊錶的交集

//小於 等於 大於

void printintersection(listnode *list1, listnode *list2)

if (cur2->date = cur2->date)

else}}

//查詢單鏈表的中間節點,要求只能遍歷一次鍊錶

//快指標 慢指標

//慢指標走一步 快指標走兩步

voidfindmid(listnode *first)

fast = fast->next;

if (fast = null)

slow = slow->next;

}printf("%d", slow->date);

}//查詢單鏈表的倒數第k個節點,要求只能遍歷一遍

void findtailk(listnode *first)

while (forward != null)

printf("%d", backward->date);

}void reverselist(listnode *list)

//result是結果

}

鏈表面試題

不改變鍊錶結構,從尾到頭列印單鏈表 遞迴實現 void printlistrevers recursively plist phead printf d phead data 當鍊表非常長的時候,遞迴實現的會導致函式呼叫層級很深,可能導致呼叫棧溢位。用棧不會出現此類情況,顯然用棧實現 的魯棒性會好一...

鏈表面試題

從尾到頭列印單鏈表 棧 遞迴 從尾到頭列印鍊錶 includevoid printtailtohead listnode head while s.empty 空間複雜度o n void printtailtoheadr listnode head cout data 刪除乙個無頭單鏈表的非尾結點 ...

鏈表面試題

typedef int datatype typedef struct node node,pnode,plist void initlist plist pplist void display const plist plist void pushback plist pplist,datatyp...