用于展平多级链表的Python程序
给定一个链表,其中除了下一个指针之外,每个节点都有一个子指针,它可能指向也可能不指向单独的链表。这些子列表可能有一个或多个自己的子列表,以此类推,以产生多级数据结构,如下图所示。您将获得列表第一级的负责人。展平列表,使所有节点出现在单级链表中。您需要以这样一种方式展平列表,即第一级的所有节点都应该首先出现,然后是第二级的节点,依此类推。
每个节点都是具有以下定义的 C 结构。
Python3
# A linked list node has data,
# next pointer and child pointer
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.child = None
# This code contributed by umadevi9616
Python3
# Python3 Program to flatten list with
# next and child pointers
# A linked list node has data,
# next pointer and child pointer
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.child = None
# Return Node
def newNode(data):
return Node(data)
# The main function that flattens
# a multilevel linked list
def flattenlist(head):
# Base case
if not head:
return
# Find tail node of first level
# linked list
temp = head
while(temp.next != None):
temp = temp.next
currNode = head
# One by one traverse through all
# nodes of first level linked list
# till we reach the tail node
while(currNode != temp):
# If current node has a child
if(currNode.child):
# then append the child
# at the end of current list
temp.next = currNode.child
# and update the tail to new
# last node
tmp = currNode.child
while(tmp.next):
tmp = tmp.next
temp = tmp
# Change current node
currNode = currNode.next
# A utility function to print
# all nodes of a linked list
def printList(head):
if not head:
return
while(head):
print("{}".format(head.data),
end = " ")
head = head.next
# Driver code
if __name__=='__main__':
# Child list of 13
child13 = newNode(16)
child13.child = newNode(3)
# Child List of 10
head1 = newNode(4)
head1.next = newNode(20)
# Child of 20
head1.next.child = newNode(2)
head1.next.next = newNode(13)
head1.next.next.child = child13
# Child of 9
child9 = newNode(19)
child9.next = newNode(15)
# Child List of 17
child17 = newNode(9)
child17.next = newNode(8)
child17.child = child9
# Child List of 7
head2 = newNode(17)
head2.next = newNode(6)
head2.child = child17
# Main List
head = newNode(10)
head.child = head1
head.next = newNode(5)
head.next.next = newNode(12)
head.next.next.next = newNode(7)
head.next.next.next.child = head2
head.next.next.next.next = newNode(11)
flattenlist(head)
print("Flattened list is: ", end = "")
printList(head)
# This code is contributed by 0_hero
上述列表应转换为 10->5->12->7->11->4->20->13->17->6->2->16->9->8->3 ->19->15
问题很明确的说,我们需要一层一层的扁平化。一个解决方案的思路是,我们从第一层开始,一个一个地处理所有节点,如果一个节点有一个孩子,那么我们将孩子追加到列表的末尾,否则,我们什么都不做。第一级处理完后,所有下一级节点都将追加到第一级之后。附加节点遵循相同的过程。
1) Take the "cur" pointer, which will point to the head
of the first level of the list
2) Take the "tail" pointer, which will point to the end of
the first level of the list
3) Repeat the below procedure while "curr" is not NULL.
I) If the current node has a child then
a) Append this new child list to the "tail"
tail->next = cur->child
b) Find the last node of the new child list and update
the "tail"
tmp = cur->child;
while (tmp->next != NULL)
tmp = tmp->next;
tail = tmp;
II) Move to the next node. i.e. cur = cur->next
以下是上述算法的实现。
Python3
# Python3 Program to flatten list with
# next and child pointers
# A linked list node has data,
# next pointer and child pointer
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.child = None
# Return Node
def newNode(data):
return Node(data)
# The main function that flattens
# a multilevel linked list
def flattenlist(head):
# Base case
if not head:
return
# Find tail node of first level
# linked list
temp = head
while(temp.next != None):
temp = temp.next
currNode = head
# One by one traverse through all
# nodes of first level linked list
# till we reach the tail node
while(currNode != temp):
# If current node has a child
if(currNode.child):
# then append the child
# at the end of current list
temp.next = currNode.child
# and update the tail to new
# last node
tmp = currNode.child
while(tmp.next):
tmp = tmp.next
temp = tmp
# Change current node
currNode = currNode.next
# A utility function to print
# all nodes of a linked list
def printList(head):
if not head:
return
while(head):
print("{}".format(head.data),
end = " ")
head = head.next
# Driver code
if __name__=='__main__':
# Child list of 13
child13 = newNode(16)
child13.child = newNode(3)
# Child List of 10
head1 = newNode(4)
head1.next = newNode(20)
# Child of 20
head1.next.child = newNode(2)
head1.next.next = newNode(13)
head1.next.next.child = child13
# Child of 9
child9 = newNode(19)
child9.next = newNode(15)
# Child List of 17
child17 = newNode(9)
child17.next = newNode(8)
child17.child = child9
# Child List of 7
head2 = newNode(17)
head2.next = newNode(6)
head2.child = child17
# Main List
head = newNode(10)
head.child = head1
head.next = newNode(5)
head.next.next = newNode(12)
head.next.next.next = newNode(7)
head.next.next.next.child = head2
head.next.next.next.next = newNode(11)
flattenlist(head)
print("Flattened list is: ", end = "")
printList(head)
# This code is contributed by 0_hero
输出:
10 5 12 7 11 4 20 13 17 6 2 16 9 8 3 19 15
时间复杂度:由于每个节点最多被访问两次,时间复杂度为 O(n),其中 n 是给定链表中的节点数。
有关详细信息,请参阅有关扁平化多级链表的完整文章!