劍指offer常用鍊錶操作

2021-07-15 01:46:56 字數 740 閱讀 7742

劍指offer中有很多的關於鍊錶的操作,現總結如下:(對於單向鍊錶,由於不能回溯,故比雙向指標要更稍微複雜些)

#include #include using namespace std;

//單鏈表的資料結構

struct list_node

;class list_solution

i++;

} return list_head;

} void print_list(list_node * list_head)

cout<<"null"while(pcurrent!=null)

listhead->next=pre;

return listhead;

} //合併兩個排序好的鍊錶

list_node* merge(list_node* listhead1,list_node* listhead2)

}else if(p2->datadata)//listhead2的值比較小

}} tem->next=(p1==nullptr)?p2:p1; //最後肯定有乙個先為空,直接把還非為空的指標接在後面

return listhead1;

}};int _tmain(int argc, _tchar* argv)

總結:在鍊錶的操作中往往會涉及到很多的指標操作,所以要特別注意出現空指標的情況,還有什麼時候到達邊界,防止可能因為一些特殊的輸入導致程式崩潰。

劍指offer 鍊錶的操作

在鍊錶中找到第乙個含有某值的節點並刪除該節點的 void addtotail listnode phead,int value phead是乙個指向指標的指標 else 在下面的鍊錶中找到第乙個含有某值的節點並刪除該節點的 void removenode listnode phead,int val...

劍指offer 鍊錶

單向鍊錶的結構定義 typedef int datatype struct listnode 問題1 往鍊錶的末尾新增乙個結點 給定頭結點,往末尾插入乙個結點 void insertnode listnode head,datatype key listnode p head while p nex...

劍指offer 鍊錶

鍊錶 鍊錶是一種動態資料結構 struct listnode 往鍊錶的末尾新增乙個節點的c 程式如下 void addtotail listnode phead,int value 注意第乙個引數phead是乙個指向指標的指標。當我們往乙個空鍊錶插入乙個結點時,else pnode m pnext ...