示例#1
0
#
# Input: [1,2,3,4,5,6]
# Output: Node 4 from this list (Serialization: [4,5,6])
# Since the list has two middle nodes with values 3 and 4, we return the second one.
#
#
# Note:
#
# The number of nodes in the given list will be between 1 and 100.

# [1,2,3,4,5,6,7] odd case
# 11, 32, 53, 74, if next is None return p2
# [1,2,3,4,5,6] even case
# 11, 32, 53,None4
from ListNode import ListNode


def middle_node(head):
    if not head:
        return None
    mid = head
    while head is not None and head.next is not None:
        print(mid, head)
        head = head.next.next
        mid = mid.next
    return mid


h = ListNode.array_to_LL([1, 2, 3, 4, 5, 6])
print([h])
示例#2
0
# works by changing all visited node's val to None
def cycle_destructive(head):
    current = head
    while current and current.val:
        current.val = None
        current = current.next
    # if no cycle, current should be None
    if not current:
        return False
    # if there is a cycle, current should be at the start of the cycle with current.val = None
    if not current.val:
        return True


def cycle_two_pointer(head):
    if not head or not head.next:
        return False
    slow, fast = head, head.next
    while slow != fast:
        if not fast or not fast.next:
            return False
        slow = slow.next
        fast = fast.next.next
    return True


x = ListNode.array_to_LL([1, 2, 3, 4])
# tail = x.next.next.next
# tail.next = x.next.next
print(cycle_slow(x))
# Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
# Output: 7 -> 8 -> 0 -> 7


from ListNode import ListNode

# construct the two integers from the linked list
def add_two_numbers_integer(l1, l2):
    n1, n2 = l1.val, l2.val
    p1, p2 = l1.next, l2.next
    while p1:
        n1 = (n1*10 + p1.val)
        p1 = p1.next
    while p2:
        n2 = (n2*10 + p2.val)
        p2 = p2.next
    s = str(n1 + n2)
    print(s)
    result = ListNode(0)
    pr = result
    for digit in s:
        pr.next = ListNode(int(digit))
        pr = pr.next
    return result.next




x = ListNode.array_to_LL([7,2,4,3])
y = ListNode.array_to_LL([5,6,4])
print(add_two_numbers_integer(x, y))
示例#4
0
            carry_over += 1
            p1.val %= 10
        p1 = p1.next
    # case 3 len(l1) < len(l2)
    if not p1.next and p2.next:
        p1.val += p2.val + carry_over
        carry_over = 0
        if p1.val >= 10:
            carry_over += 1
            p1.val %= 10
        p1.next = p2.next
        p1 = p1.next
    # last part to manage carryover for last digit
    while p1.next:
        p1.val += carry_over
        carry_over = 0
        if p1.val >= 10:
            carry_over += 1
            p1.val %= 10
        p1 = p1.next
    p1.val += carry_over
    if p1.val >= 10:
        p1.val %= 10
        p1.next = ListNode(1)
    return l1


a = ListNode.array_to_LL([4,4,4])
b = ListNode.array_to_LL([3,3,6,9,9,9])
print(add_two_nums(a, b))
示例#5
0
    n = head.next.next
    p.next = None
    while n is not None:
        c.next = p
        p = c
        c = n
        n = n.next
    c.next = p
    return c

def reverse_concise_iterative(head):
    p = None
    c = head
    while c is not None:
        n = c.next
        c.next = p
        p = c
        c = n
    return p

def reverse_recursive(head):
    def recurse(prev, head):
        if head is None:
            return prev
        n = head.next
        head.next = prev
        return recurse(head, n)
    return recurse(None, head)

x = ListNode.array_to_LL([1])
print(reverse_recursive(None))
# 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 ListNode import ListNode


def bin_LL_to_dec(head):
    exp = 0
    d = []
    current = head
    while current:
        d.append([current.val, exp])
        exp += 1
        current = current.next
    max_exp = exp - 1
    print(max_exp)
    for digit in d:
        digit[1] = max_exp - digit[1]
    return sum([digit * 2**exp for digit, exp in d])


x = ListNode.array_to_LL([1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0])
print(bin_LL_to_dec(x))
# In this method, an additional loop is skip through all the duplicates, but is messy with all the checks
def remove_duplicates(head):
    if not head:
        return None
    current = head
    while current is not None and current.next is not None:
        while current.next is not None and current.val != current.next.val:
            current = current.next
        dup = current.next
        while dup is not None and dup.next is not None and dup.val == dup.next.val:
            dup = dup.next
        if dup is not None:
            current.next = dup.next
        current = current.next
    return head


# Much cleaner method. My just repetitively skipping and only moving current when there isn't a duplicate, we can
# perform the same operation using much more concise code
def remove_duplicates_cleaner(head):
    current = head
    while current is not None and current.next is not None:
        if current.val == current.next.val:
            current.next = current.next.next
        else:
            current = current.next
    return head


x = ListNode.array_to_LL([1, 2])
print(remove_duplicates_cleaner(x))