# Input: head = [1] # Output: 1 # # Example 4: # Input: head = [1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0] # Output: 18880 # # Example 5: # Input: head = [0, 0] # Output: 0 # # Constraints: # The Linked List is not empty. # Number of nodes will not exceed 30. # Each node 's value is either 0 or 1. from LinkedListx import linked_list def get_decimal_value(head): num = head.val print(num) while head.next: num = num * 2 + head.next.val print(num) head = head.next return num list1 = linked_list([1, 1, 1, 1]) print(get_decimal_value(list1))
# # The number of nodes in the list is n. # 1 <= k <= n <= 105 # 0 <= Node.val <= 100 from LinkedListx import linked_list def swap_nodes(head, k): curr = head knode2 = head for _ in range(k - 1): curr = curr.next knode1 = curr while curr.next is not None: curr = curr.next knode2 = knode2.next knode1.val, knode2.val = knode2.val, knode1.val return head list1 = linked_list([1, 2, 3, 4, 5]) print(list1) print(swap_nodes(list1, 2)) list1 = linked_list([1]) print(list1) print(swap_nodes(list1, 1))
def swap_pairs(head): if head is None: return None if head.next is None: return head curr = head while True: curr.val, curr.next.val = curr.next.val, curr.val if curr.next.next is None or curr.next.next.next is None: break curr = curr.next.next return head list1 = linked_list([1]) print(list1) print(swap_pairs(list1)) list1 = linked_list([1, 2]) print(list1) print(swap_pairs(list1)) list1 = linked_list([1, 2, 3]) print(list1) print(swap_pairs(list1)) list1 = linked_list([1, 2, 3, 4]) print(list1) print(swap_pairs(list1))
# continue # prev = curr # curr = curr.next # # return head # list1 = linked_list([1, 4, 3, 2, 5, 2]) # print(list1) # print(partition(list1, 3)) # list1 = linked_list([2, 1]) # print(list1) # print(partition(list1, 2)) # list1 = linked_list([1, 1]) # print(list1) # print(partition(list1, 0)) list1 = linked_list([5, 4, 3, 2, 1]) print(list1) print(partition(list1, 3)) list1 = linked_list([5]) print(list1) print(partition(list1, 3)) list1 = linked_list([]) print(list1) print(partition(list1, 3))
# return head # class Solution: # def oddEvenList(self, head: ListNode) -> ListNode: # cur = head # values = [] # while cur: # values.append(cur.val) # cur = cur.next # # i = 0 # cur = head # while i < len(values): # cur.val = values[i] # cur = cur.next # i += 2 # i = 1 # while i < len(values): # cur.val = values[i] # cur = cur.next # i += 2 # return head # 1 2 3 4 5 # 1 3 2 4 5 move 2 to 1 # 1 3 5 2 4 move 4 to 2 list1 = linked_list([1, 2, 3, 4, 5, 6, 7]) print(list1) print(odd_even_list(list1))
# while curr: # nodes.append(curr.val) # curr = curr.next # # curr = head # while curr: # if curr.val != nodes.pop(): # return False # curr = curr.next # # return True def is_palindrome(head): arr = [] curr = head while curr: arr.append(curr.val) curr = curr.next n = len(arr) for i in range(n // 2): if arr[i] != arr[n - 1 - i]: return False return True list1 = linked_list([1, 2, 3, 2, 1]) print_list(list1) print(is_palindrome(list1))
def print_list(curr): while curr: print(curr.val, end=' ') curr = curr.next print('') def get_intersection_node(heada, headb): seta = set() curr = heada while curr: seta.add(curr) curr = curr.next curr = headb while curr: if curr in seta: return curr curr = curr.next return None list1 = linked_list([1, 2, 6, 7, 8]) print(list1) list2 = linked_list([3, 4, 5]) list2.next.next.next = list1.next.next print(list2) print((get_intersection_node(list1, list2)))
while curr: print(curr.val, end=' ') curr = curr.next def get_list_value(head): list_val = 0 i = 0 curr = head while curr: list_val += curr.val * 10**i i += 1 curr = curr.next return list_val def add_two_numbers(l1, l2): sumx = get_list_value(l1) + get_list_value(l2) node_vals = [int(d) for d in reversed(str(sumx))] head = prev = ListNode(node_vals[0]) for val in node_vals[1:]: prev.next = ListNode(val) prev = prev.next return head list1 = linked_list([2, 4, 3]) list2 = linked_list([5, 6, 4]) print_list(add_two_numbers(list1, list2))
# else: # head = curr2 # # while curr1 and curr2: # next1 = curr1.next # next2 = curr2.next # if curr1.val < curr2.val: # curr1.next = curr2 # else: # curr2.next = curr1 # # if next1 is None: # tail1 = curr1 # if next2 is None: # tail2 = curr2 # # curr1 = next1 # curr2 = next2 # # if curr1: # tail2.next = curr1 # # if curr2: # tail1.next = curr2 # # return head list1 = linked_list([3, 5]) list2 = linked_list([1, 2, 4]) print_list(merge_two_lists(list1, list2))
# class Solution: # def removeElements(self, head, val): # """ # :type head: ListNode # :type val: int # :rtype: ListNode # """ # # dummy_head = ListNode(-1) # dummy_head.next = head # # current_node = dummy_head # while current_node.next != None: # if current_node.next.val == val: # current_node.next = current_node.next.next # else: # current_node = current_node.next # # return dummy_head.next # list1 = linked_list([6, 6, 6, 6, 6, 1, 2, 6, 3, 4, 5, 6]) list1 = linked_list([1, 2, 6, 3, 4, 5, 6]) print(list1) print(remove_elements(list1, 6)) list1 = linked_list([1, 2, 2, 1]) print(list1) print(remove_elements(list1, 2))