Beispiel #1
0
def partition(sll, x):
  upper = singly_linked_list()
  lower = singly_linked_list()
  node = sll.head
  while node:
    next_node = node.next_node
    node.next_node = None
    if node.data < x:
      lower.append_node(node)
    else:
      upper.append_node(node)
    node = next_node

  lower.append_node(upper.head)
  return lower
Beispiel #2
0
def partition(sll, x):
    upper = singly_linked_list()
    lower = singly_linked_list()
    node = sll.head
    while node:
        next_node = node.next_node
        node.next_node = None
        if node.data < x:
            lower.append_node(node)
        else:
            upper.append_node(node)
        node = next_node

    lower.append_node(upper.head)
    sll.head = lower.head
    return sll
Beispiel #3
0
def add_lists(l1, l2):
  len1 = len(l1)
  len2 = len(l2)
  maxlen = max([len1, len2])
  l1 = pad_with_zeros(l1, maxlen - len1)
  l2 = pad_with_zeros(l2, maxlen - len2)
  result_tail, carry = add_lists_same_length(l1.head, l2.head)
  if carry > 0:
    result_head = singly_linked_list_node(carry, result_tail)
  else:
    result_head = result_tail

  result = singly_linked_list()
  result.append_node(result_head)
  return result
Beispiel #4
0
def add_lists(l1, l2):
    len1 = len(l1)
    len2 = len(l2)
    maxlen = max([len1, len2])
    l1 = pad_with_zeros(l1, maxlen - len1)
    l2 = pad_with_zeros(l2, maxlen - len2)
    result_tail, carry = add_lists_same_length(l1.head, l2.head)
    if carry > 0:
        result_head = singly_linked_list_node(carry, result_tail)
    else:
        result_head = result_tail

    result = singly_linked_list()
    result.append_node(result_head)
    return result
Beispiel #5
0
def is_palindrome(l):
  list_len = len(l)
  first_half_reversed = singly_linked_list()
  node = l.head
  for i in xrange(list_len / 2):
    first_half_reversed.insert(0, node.data)
    node = node.next_node

  if list_len % 2 > 0:
    node = node.next_node

  reverse_node = first_half_reversed.head

  for i in xrange(list_len / 2):
    if node.data != reverse_node.data:
      return False
    node = node.next_node
    reverse_node = reverse_node.next_node

  return True
Beispiel #6
0
def add_lists(l1, l2):
  result = singly_linked_list()
  node1 = l1.head
  node2 = l2.head
  carry = 0
  while node1 or node2:
    if node1 and node2:
      result.append((node1.data + node2.data + carry) % 10)
      carry = (node1.data + node2.data + carry) / 10
    else:
      node = node1 or node2
      result.append((node.data + carry) % 10)
      carry = (node.data + carry) / 10

    if node1:
      node1 = node1.next_node
    if node2:
      node2 = node2.next_node
  
  if carry:
    result.append(carry)
  return result
Beispiel #7
0
def add_lists(l1, l2):
    result = singly_linked_list()

    node1 = l1.head
    node2 = l2.head
    carry = 0
    while node1 or node2:
        if node1 and node2:
            result.append((node1.data + node2.data + carry) % 10)
            carry = (node1.data + node2.data + carry) / 10
        else:
            node = node1 if node1 else node2
            result.append((node.data + carry) % 10)
            carry = (node.data + carry) / 10

        node1 = node1.next_node if node1 else None
        node2 = node2.next_node if node2 else None

    if carry > 0:
        result.insert(0, carry)

    return result