o 1 時間刪除鍊錶的節點

2021-07-04 20:08:23 字數 635 閱讀 2609

題目:給定單向鍊錶的頭指標和乙個節點指標,定義乙個函式在o(1)時間內刪除該節點。

#include #include "list.h"

using namespace std;

void deletenode(listnode **phead, listnode *pnode)

else if(*phead == pnode)

else

}

// ********************測試**********************

void test(listnode* plisthead, listnode* pnode)

// 鍊錶中有多個結點,刪除中間的結點

void test1()

// 鍊錶中有多個結點,刪除尾結點

void test2()

// 鍊錶中有多個結點,刪除頭結點

void test3()

// 鍊錶中只有乙個結點,刪除頭結點

void test4()

// 鍊錶為空

void test5()

int main(int argc, char* argv)

O(1)時間刪除鍊錶節點

問題描述 給定單相鍊錶的頭指標和乙個節點指標,定義乙個函式在o 1 時間刪除該節點。這個比較簡單,做不做解釋,直接看參考 不過有一點就是要注意,還是要看刪除的節點型別,不能保證總是o 1 時間 void deletenode listnode phead,listnode ptobedelete 刪...

69 在O 1 時間刪除鍊錶節點

對於刪除鍊錶節點需要考慮的問題 鍊錶為空時?刪除頭節點時?刪除尾節點時?要在o 1 o 1 的時間複雜度內刪除節點,那只能採用特殊辦法了 對於頭節點,很容易完成,因為它沒有前驅 對於中間節點,只能是把待刪除節點改造成其後繼節點,然後刪除後繼節點了,這樣值是相等的,但是確實不是同乙個節點 對於尾節點,...

O 1 時間刪除鍊錶結點

題目 給定鍊錶的頭指標和乙個結點指標,在 o 1 時間刪除該結點。鍊錶結點的定義如下 struct listnode 函式的宣告如下 void deletenode listnode plisthead listnode ptobedeleted 分析 這是一道廣為流傳的 google 面試題,能有...