Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 20 additions & 31 deletions [B]linked-list/[B]linked-list/25-reverse-nodes-in-k-group.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,35 @@
class Solution:
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:


def rev_group(prev_nodes, old_start, old_end, next_nodes):

def rev(prev_nodes, old_head, old_tail, next_nodes):
prev = None
cur = old_start
cur = old_head
while cur and cur != next_nodes:
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
new_start = prev
new_end = old_start
prev_nodes.next = new_start
new_end.next = next_nodes

return new_end, next_nodes


dummy = ListNode()
dummy.next = head
new_head = prev
prev_nodes.next = new_head
new_tail = old_head
new_tail.next = next_nodes
return new_tail

prev = dummy
dummy = ListNode(next=head)
total = 0
prev_nodes = dummy
old_head = head
cur = head
prev_nodes = None
old_start = None
old_end = None
next_nodes = None
count = 0
while cur:
count += 1
if count == 1:
prev_nodes = prev
old_start = cur
if count == k:
old_end = cur
next_nodes = cur.next
prev, cur = rev_group(prev_nodes, old_start, old_end, next_nodes)
count = 0
else:
prev, cur = cur, cur.next
total += 1
if total == 1:
old_head = cur
if total == k:
new_tail = rev(prev_nodes, old_head, cur, cur.next)
prev_nodes = new_tail
cur = new_tail
total = 0
cur = cur.next
return dummy.next

# time O(n)
Expand Down
16 changes: 7 additions & 9 deletions [B]linked-list/linked-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,19 @@ prev and cur
return prev

# version 2
def reverse_nodes(self, prev_nodes, old_start, old_end, next_nodes):

def reverse_nodes(prev_nodes, old_head, old_tail, next_nodes):
prev = None
cur = old_start
cur = old_head
while cur and cur != next_nodes:
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
new_start = prev
new_end = old_start
prev_nodes.next = new_start
new_end.next = next_nodes

return new_end, next_nodes
new_head = prev
prev_nodes.next = new_head
new_tail = old_head
new_tail.next = next_nodes
return new_tail
```

- swap nodes in pairs
Expand Down
25 changes: 16 additions & 9 deletions [K]graph/[K]graph-bfs/133-clone-graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ def __init__(self, val = 0, neighbors = None):
class Solution:
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
if not node:
return None
old_new = {node: Node(node.val)}
queue = deque([node])
return node
old_new = {}
queue = deque([])
visited = set()
queue.append(node)
visited.add(node)
while queue:
old = queue.popleft()
for old_neighbor in old.neighbors:
if old_neighbor not in old_new:
old_new[old_neighbor] = Node(old_neighbor.val)
queue.append(old_neighbor)
old_new[old].neighbors.append(old_new[old_neighbor])
old_node = queue.popleft()
if old_node not in old_new:
old_new[old_node] = Node(old_node.val)
for neighbor in old_node.neighbors:
if neighbor not in old_new:
old_new[neighbor] = Node(neighbor.val)
old_new[old_node].neighbors.append(old_new[neighbor])
if neighbor not in visited:
queue.append(neighbor)
visited.add(neighbor)
return old_new[node]

# time O(n)
Expand Down