This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def reverse_list(prev, current, next_group_pointer): | |
| while current != next_group_pointer: | |
| next_node = current.next | |
| current.next = prev | |
| prev = current | |
| current = next_node | |
| return prev, current |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def swap_node_pairs(head): | |
| if not head or not head.next: | |
| return head | |
| dummy_node = ListNode() | |
| dummy_node.next = head | |
| prev = dummy_node | |
| i = head | |
| j = head.next |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def remove_nth_node(head, n): | |
| i_slow = head | |
| j_fast = head | |
| # first we will move the fast pointer so that the distance between | |
| # j_fast - j_slow = n | |
| for _ in range(n): | |
| j_fast = j_fast.next |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_pointers_meetingPoint(head): | |
| slow = head | |
| fast = head | |
| while fast and fast.next: | |
| slow = slow.next | |
| fast = fast.next.next | |
| if slow == fast: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def list_has_cycle(head): | |
| if not head: | |
| return False | |
| fast_p = head.next | |
| slow_p = head | |
| while fast_p: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def reverse_list(head): | |
| if not head: return None | |
| prev_pointer = None | |
| current_pointer = head | |
| while current_pointer: | |
| next_pointer = current_pointer.next |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def is_balanced_BT(root): | |
| def check_balance(root): | |
| if not root: | |
| return [True, 0] | |
| left_height_component = check_balance(root.left) | |
| right_height_component = check_balance(root.right) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def is_symmetric_BT(root): | |
| def check(left, right): | |
| if not left and not right: | |
| return True | |
| if not left or not right: | |
| return False | |
| if left.val != right.val: | |
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def is_same_BT(root1, root2): | |
| def check(root1, root2): | |
| if not root1 and not root2: # check for the end of both trees. | |
| return True | |
| if not root1 or not root2: # Check for the heights' similarity. | |
| return False | |
| if root1.val != root2.val: # check for the key values' similarity. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def isvalid_BT(root): | |
| def Inorder(root, bag): | |
| if not root: | |
| return [] | |
| if not root.left and not root.right: | |
| return bag.append(root.val) | |
| if not root.left: | |
| bag.append(root.val) | |
| return Inorder(root.right, bag) |
NewerOlder