複雜鍊錶的複製

2021-10-22 05:42:16 字數 1186 閱讀 3479

請實現 copyrandomlist 函式,複製乙個複雜鍊錶。在複雜鍊錶中,每個節點除了有乙個 next 指標指向下乙個節點,還有乙個 random 指標指向鍊錶中的任意節點或者 null。

示例 1:

輸入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]

輸出:[[7,null],[13,0],[11,4],[10,2],[1,0]]

示例 2:

輸入:head = [[1,1],[2,1]]

輸出:[[1,1],[2,1]]

示例 3:

輸入:head = [[3,null],[3,0],[3,null]]

輸出:[[3,null],[3,0],[3,null]]

示例 4:

輸入:head =

輸出:解釋:給定的鍊錶為空(空指標),因此返回 null。

/*

// definition for a node.

class node

};*/

class

solution

map,int

> m;

//標記當前位址對應的的鍊錶中的第幾個結點 比如第乙個位址就代表第乙個結點位置

vector

> v;

//建立新的鍊錶結點

node* cur=head;

int i=0;

while

(cur)

v.push_back(0

);//為了尾部操作統一化

cur=head;

node* headnew=v[0]

; i=0;

while

(cur)

i++; cur=cur-

>next;

}return headnew;}}

;

鍊錶 複雜鍊錶的複製

問題描述 請實現函式complexlistnode clone complexlistnode phead 複製乙個複雜鍊錶。在複雜鍊錶中,每個結點除了有乙個next指標指向下乙個結點之外,還有乙個random指向鍊錶中的任意結點或者null。結點的定義如下 struct randomlistnod...

複雜鍊錶複製

複雜鍊錶複製的標頭檔案mlist.h ifndef mlist h define mlist h include include includetypedef int datatype typedef struct node node,pnode,plist pnode crealist datat...

複製複雜鍊錶

題目 輸入乙個複雜鍊錶 每個節點中有節點值,以及兩個指標,乙個指向下乙個節點,另乙個特殊指標指向任意乙個節點 返回結果為複製後複雜鍊錶的head。注意,輸出結果中請不要返回引數中的節點引用,否則判題程式會直接返回空 解題思路 首先有三種解法 第一種就是中規中矩的解法,首先複製next指標的節點,之後...