week6‐week7 - Joanna-0427/leetcode GitHub Wiki

Problem 5: Mirror, Mirror

You think another bit of wonky spell casting may have left your enchanted mirror broken. Write a function is_mirrored() to test if your mirror successfully reflects objects back. The function accepts the head of a linked list and should return True if the values of the linked list read the same backwards and forwards, and False otherwise.

Evaluate the time and space complexity of your solution. Define your variables and provide a rationale for why you believe your solution has the stated time and space complexity.

class Node: def init(self, value, next=None): self.value = value self.next = next

For testing def print_linked_list(head): current = head while current: print(current.value, end=" -> " if current.next else "\n") current = current.next

def is_mirrored(head): pass

Problem 6: Magic Loop

In a nearby enchanted forest, magical paths sometimes loop back on themselves, creating never-ending cycles. Write a function loop_start() to help you keep your way. The function accepts the head of a linked list path_start and returns the value of the node where the cycle starts. If the path has no cycle, return None.

A linked list has a cycle if, at some point in the list, the node’s next pointer points back to a previous node in the list.

Evaluate the time and space complexity of your solution. Define your variables and provide a rationale for why you believe your solution has the stated time and space complexity.

class Node: def init(self, value, next=None): self.value = value self.next = next

For testing def print_linked_list(head): current = head while current: print(current.value, end=" -> " if current.next else "\n") current = current.next

def loop_start(path_start): pass