#430 Flatten a Multilevel Doubly Linked List
You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional child pointer. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure as shown in the example below.
Given the
head
of the first level of the list, flatten the list so that all the nodes appear in a single-level, doubly linked list. Letcurr
be a node with a child list. The nodes in the child list should appear aftercurr
and beforecurr.next
in the flattened list.Return the
head
of the flattened list. The nodes in the list must have all of their child pointers set tonull
.
Example 1:
解题思路:
1. 有子节点就往下走,没有就往前走。
2.往前走到尽头,接回上一层母节点的下一个节点。
重复1,2步直到所有节点走完为止。
因为涉及到接回上一层母节点的下一个节点,我们要用到marker pointer防止链后面的节点丢失。有几个母节点就需要几个marker。这种嵌套处理我本来很想用recursive思路。但是写了好久都没有写出来。
只能用iterative的思路:
marker信息全部放在stack里存着,等到当前点跑到某个链的尽头时,再调用stack里的最后一个接上。
"""
# Definition for a Node.
class Node:
def __init__(self, val, prev, next, child):
self.val = val
self.prev = prev
self.next = next
self.child = child
"""
class Solution:
def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
if not head: return head
curr = head
stack = []
while curr:
nextNode = curr.next
childNode = curr.child
if childNode:
if nextNode: stack.append(nextNode) # ensure no None inside
curr.next = childNode
childNode.prev = curr
curr.child = None
curr = childNode
else:
if not nextNode: # hit the end
if stack: # if stack has unlinked element
temp = stack.pop()
curr.next = temp
temp.prev = curr
else: # no more element in the stack
break
curr = curr.next
return head
runtime:
recursive实在太难了。看了forum的解答也是理解不了。目前水平脑子真转不过来(做不到跳步思考)。只能先放着,回头在看。
#708 Insert into a Cyclic S
Given a Circular Linked List node, which is sorted in ascending order, write a function to insert a value
insertVal
into the list such that it remains a sorted circular list. The given node can be a reference to any single node in the list and may not necessarily be the smallest value in the circular list.If there are multiple suitable places for insertion, you may choose any place to insert the new value. After the insertion, the circular list should remain sorted.
If the list is empty (i.e., the given node is
null
), you should create a new single circular list and return the reference to that single node. Otherwise, you should return the originally given node.
解题思路:
先创建new node。如果head为null,则new.next = new之后return new。
考虑是环链,新点可以插入的情况分为三种:
1)当cur.val 小于 cur.next.val时, 在cur.val和cur.next.val之间
2)当cur.val比cur.next.val大时,位于交界处。要么比两个值都大,要么比两个值都小。
3)环里只有一个点或环里所有点的值都相等
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, next=None):
self.val = val
self.next = next
"""
class Solution:
def insert(self, head: 'Optional[Node]', insertVal: int) -> 'Node':
cur = head
new = Node(insertVal)
if not head:
new.next = new
return new
while cur:
# if it is one-node circular linked list
# or the values of all nodes are the same
if cur.next.val == cur.val:
break
if cur.val <= insertVal <= cur.next.val:
break
if cur.val > cur.next.val and (insertVal > cur.val or insertVal < cur.next.val):
break
cur = cur.next
new.next = cur.next
cur.next = new
return head
runtime:
medium的题真的怕了,有些连思路都很难理出来,有些是edgecase很复杂,要很小心处理。