鏈表面試題2

2021-08-18 17:39:21 字數 3139 閱讀 9769

有關單鏈表的面試題2

複雜鍊錶的複製

//複雜鍊錶的複製

typedef struct complexnodecomplexnode;

complexnode* createcomplexnode(linknodetype value)

size_t diff(complexnode* src , complexnode* dest)

++offset;

src=src->next;

} if(src==null)

return offset;

}complexnode* step(complexnode* head ,size_t offset)

if(i>=offset)

++i;

cur = cur->next;

} return null;

}complexnode* copycomplexlist(complexnode* head)

//先按照簡單鍊錶來進行複製

complexnode* new_head = null;

complexnode* new_tail = null;

complexnode* cur = head;

for(;cur != null;cur = cur->next)else

} //遍歷舊鍊錶,找到每個鍊錶節點random指標相對與鍊錶頭部的偏移量

//遍歷新鍊錶,根據偏移量,設定新鍊錶的random

complexnode* new_cur = new_head;

for(cur = head;cur!=null;cur=cur->next,new_cur=new_cur->next)

//通過diff函式來計算兩個節點的偏移量

size_t offset = diff(head ,cur->random);

//通過step函式,相當與求出從new_head出發,走了offset步,到達的位置是誰

new_cur->random = step(new_head,offset);

} return new_head;

}

兩個有序鍊錶求交集

linknode* linklistunionset(linknode* head1 , linknode* head2) 

linknode* cur1 = head1;

linknode* cur2 = head2;

linknode* new_head = null;

linknode* new_tail = null;

while(cur1 != null && cur2 != null) else if(cur1->data > cur2->data) else else

cur1 = cur1->next;

cur2 = cur2->next;

} }return new_head;

}

判斷兩個單鏈表是否相交(可能帶環)

int linklisthascrosswithcycle(linknode* head1 , linknode* head2) 

//求連個鍊錶的入口點

linknode* entry1 = linklistcycleentry(head1);

linknode* entry2 = linklistcycleentry(head2);

//如果兩個鍊錶都不帶環,則按照前面的方法判定

if(entry1 == null && entry2 == null)

//如果乙個帶環乙個不帶環,則不相交

if((entry1 == null && entry2 != null)||(entry1 != null && entry2 == null))

//如果兩個鍊錶都帶環

//a)如果連個入口相同,說明相交,並且是環外相交

if(entry1 == entry2)

//b)如果從乙個入口點出發,繞環一周,能到達第二個入口點說明也是相交,並且是環上相交

linknode* cur = entry1->next;

while(cur != entry1)

cur = cur->next;

} return 0;

}

求可能帶環的兩個鍊錶相交的交點

linknode* linklisthascrosswithcyclepos(linknode* head1 , linknode* head2) 

//交點在環外

linknode* entry1 = linklistcycleentry(head1);

linknode* entry2 = linklistcycleentry(head2);

if(entry1 == entry2)

for(cur2 = head2 ;cur2 != entry2;cur2 = cur2->next)

if(len1 > len2)

while(cur1 != null && cur2 != null)

cur1 = cur1->next;

cur2 = cur2->next;

}}else

while(cur1 != null && cur2 != null)

cur1 = cur1->next;

cur2 = cur2->next;

}} }

//環上相交

if(entry1 != entry2)

}

測試**:

void testhascrosswithcyle()

void testunionset()

void printcomplexlist(complexnode* head ,const char* msg)

printf("\n");

for(cur=head;cur!=null;cur=cur->next)

printf("[%c] ",cur->random->data);

} printf("\n");

}void testcomplexlistcopy()

鏈表面試題2

2 反轉鍊錶 定義乙個函式,輸入乙個鍊錶的頭節點,反轉該鍊錶並輸出反轉後鍊錶的頭節點。示例 輸入 1 2 3 4 5 null 輸出 5 4 3 2 1 null class solution cur.next prev prev cur cur curnext return newhead 反轉鍊...

鏈表面試題

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

鏈表面試題

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