劍指Offer 35 複雜鍊錶的複製

2021-09-09 06:08:50 字數 3613 閱讀 6348

請實現啊函式complexlistnode* clone(complexlistnode* phead),複製乙個

複雜鍊錶。在複雜鍊錶中除了有乙個m_pnext指標指向下乙個節點,還有乙個m_psaibling

指標指向鍊錶中的任意節點或者nullptr。

節點定義如下:

class

complexlistnode

(object):

def__init__

(self, x)

: self.val = x

self.

next

=none

self.sibling =

none

第一步,複製主鏈,解決所有節點的next配置。

第二步,解決silbing配置,遍歷舊鍊錶,如果有sibling,則舊煉表頭遍歷尋找該節點,記錄離煉表頭的距離後在新煉表裡同樣設定。

時間複雜度:o(n^2)

空間複雜度:o(1)

第一步,複製主鏈,解決next,同時設定hash表,將舊節點和新節點建立對映。

第二步,解決sibling,node遍歷舊鍊錶,當有node.sibling時,新鍊錶new_node.sibling設定為hash表中node.sibling對映的結果。

時間複雜度:o(n)

空間複雜度:o(n)

第一步,將主鏈每個節點都複製乙個拷貝連線入原節點的後面。a-b -> a-a』-b-b』

第二步,將按原節點設定複製節點的sibling,有node』.sibling = node.sibling.next

第三步,將鍊錶分解成新舊兩條,奇數鏈和偶數鏈。

時間複雜度:o(n)

2空間複雜度:o(1)

def

complex_list_node_clone

(head)

:"""

:param head: origin head

:return:copy head

"""sham_head = complexlistnode(0)

new_node = sham_head

node = head

reflect =

while node:

new_node.

next

= complexlistnode(node.val)

new_node = new_node.

next

reflect[node]

= new_node

node = node.

next

node = head

new_node = sham_head.

next

while node:

if node.sibling:

new_node.sibling = reflect[node.sibling]

node = node.

next

new_node = new_node.

next

return sham_head.

next

def

complex_list_node_clone_2

(head)

:"""

:param head: origin head

:return:copy head

"""node = head

while node:

new_node = complexlistnode(node.val)

node.

next

, node = new_node, node.

next

new_node.

next

= node

node = head

while node:

if node.sibling:

node.

next

.sibling = node.sibling.

next

node = node.

next

.next

node = head

copy_head = head.

next

while node.

next

: node.

next

, node = node.

next

.next

, node.

next

return copy_head

思路3其實和思路2異曲同工,就是建立新舊節點的對映關係,以便處理sibling屬性,而不用再次從頭查詢。思路2採用額外雜湊表使查詢時間為o(1),思路3則將新節點特別的安排為舊節點的next,這樣對映關係即為new_node = node.next 也是o(1)。

# definition for singly-linked list with a random pointer.

# class randomlistnode(object):

# def __init__(self, x):

# self.label = x

# self.next = none

# self.random = none

class

solution

(object):

defcopyrandomlist

(self, head, hash_table=):

""" :type head: randomlistnode

:rtype: randomlistnode

"""ifnot head:

return

none

if head in hash_table:

return hash_table[head]

hash_table[head]

= randomlistnode(head.label)

hash_table[head]

.random = self.copyrandomlist(head.random, hash_table)

hash_table[head]

.next

= self.copyrandomlist(head.

next

, hash_table)

return hash_table[head]

上面的**是別人的解法。很有意思因此分享

他的思路很特別,不是分步解決next 和 sibling屬性,而是在一次遍歷中解決。

具體操作如下:

遍歷鍊錶,對於乙個節點一次性設定完next和sibiling屬性,將設定好的節點存入字典

對於next和sibling這兩條支鏈,都遞迴設定,直到遇到設定好的節點或者鍊錶到頭。

在字典**現的節點,意味著此節點以後的所有節點都已經被設定完畢。

這份**採用了遞迴,十分簡潔。基本思路也比較清晰,就是只要next和sibling還未配置就一路配置到配置完成為止。雜湊表記錄是否配置過用來防止死迴圈。

(劍指offer)35 複雜鍊錶的複製

輸入乙個複雜鍊錶 每個節點中有節點值,以及兩個指標,乙個指向下乙個節點,另乙個特殊指標指向任意乙個節點 返回結果為複製後複雜鍊錶的head。注意,輸出結果中請不要返回引數中的節點引用,否則判題程式會直接返回空 思路 1.先複製鍊錶節點的值放在原來的節點後面,組成乙個新的鍊錶 2.處理複雜指標 安排複...

劍指offer35 複雜鍊錶的複製

輸入乙個複雜鍊錶 每個節點中有節點值,以及兩個指標,乙個指向下乙個節點,另乙個特殊指標指向任意乙個節點 返回結果為複製後複雜鍊錶的head。注意,輸出結果中請不要返回引數中的節點引用,否則判題程式會直接返回空 剛開始看題時還有點摸不著頭腦,感覺這道題沒有任何存在的意義。後來看了大家的討論才明白。思想...

劍指offer 35 複雜鍊錶的複製

面試題35.複雜鍊錶的複製 難度中等33 請實現 copyrandomlist 函式,複製乙個複雜鍊錶。在複雜鍊錶中,每個節點除了有乙個 next 指標指向下乙個節點,還有乙個 random 指標指向鍊錶中的任意節點或者 null。示例 1 輸入 head 7,null 13,0 11,4 10,2...