輸入乙個複雜鍊錶(每個節點中有節點值,以及兩個指標,乙個指向下乙個節點,另乙個特殊指標指向任意乙個節點),返回結果為複製後複雜鍊錶的head。(注意,輸出結果中請不要返回引數中的節點引用,否則判題程式會直接返回空)
思路: 1.先根據next複製整個鍊錶.2.根據random指標在原始鍊錶的位置找出random在複製的鍊錶中對應的位置, 並建立關聯.
測試struct randomlistnode
};class solution
head = new randomlistnode(phead->label);
randomlistnode* sourcehead = phead;
randomlistnode* targethead = head;
// 複製next
while (null != sourcehead && null != sourcehead->next)
sourcehead = phead;
targethead = head;
// 複製random
while (null != sourcehead)
randomlistnode* random = sourcehead->random;
randomlistnode* searchstart = phead;
randomlistnode* node = head;
while (searchstart != null && node !=null)
searchstart = searchstart->next;
node = node->next;
}targethead->random = node;
sourcehead = sourcehead->next;
targethead = targethead->next;
}} while (0);
return head;
}};
鍊錶 複雜鍊錶的複製
問題描述 請實現函式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指標的節點,之後...