def merge_two_lists(l1, l2):
    l3 = Node(None)
    l3head = l3

    while l1 and l2:
        if l1.value < l2.value:
            l3.next = l1
            l1 = l1.next
        else:
            l3.next = l2
            l2 = l2.next
        l3 = l3.next

    l3.next = l1 or l2
    return l3head.next
Esempio n. 2
0
 def test_remove_data(self):
     """测试删除(通过数据值)节点"""
     link = LinkedList(head)
     node1 = Node(3)
     node2 = Node(3)
     node3 = Node(2)
     link.head.next = node1
     node1.next = node2
     node2.next = node3
     link.remove_by_data(3)
     self.assertTrue(link.get_size() == 2)
     self.assertTrue(link.get_data(2) == None)
     self.assertTrue(link.get_data(1) == 2)
     link.remove_by_data('ddd')
     self.assertTrue(link.get_size() == 2)
Esempio n. 3
0
def swapPairs(head: Node) -> Node:
    if head and head.next:
        p = head.next
        head.next = swapPairs(p.next)
        p.next = head
        return p
    return head
Esempio n. 4
0
 def test_insert_data(self):
     """测试插入(通过数据值)节点"""
     link = LinkedList(head)
     node1 = Node(2)
     node2 = Node(3)
     node3 = Node(3)
     link.head.next = node1
     node1.next = node2
     node2.next = node3
     link.insert_by_data(10, 3)
     self.assertTrue(link.get_size() == 6)
     self.assertTrue(link.get_data(2) == 10)
     self.assertTrue(link.get_data(4) == 10)
     node4 = Node(1)
     link.insert_by_data(node4, 10)
     self.assertTrue(link.get_index(node4) == [2, 5])
def test():
    n1 = Node(3)
    n1.next = Node(7)
    n1.next.next = Node(8)
    n1.next.next.next = Node(10)
    n1.next.next.next.next = Node(15)
    n1.printList()

    n2 = Node(1)
    n2.next = Node(2)
    n2.next.next = Node(10)
    n2.next.next.next = Node(11)
    n2.printList()

    n3 = mergeSortedLinkedLists(n1, n2)
    n3.printList()
 def InsertAtBeginning(self, data):
     if self.head == None:
         print("EMpty linked list.")
     else:
         new = Node(data)
         new.next = self.head
         self.head = new
Esempio n. 7
0
def addTwoNums(ln1, ln2):
    ln1stack = buildStack(ln1)
    ln2stack = buildStack(ln2)

    head = Node()
    carry = 0
    while len(ln1stack) > 0 or len(ln2stack) > 0 or carry != 0:
        x = 0 if len(ln1stack) == 0 else ln1stack.pop().data
        y = 0 if len(ln2stack) == 0 else ln2stack.pop().data
        sum = x + y + carry
        node = Node(sum % 10)
        node.next = head.next
        head.next = node
        carry = sum // 10

    return head.next
Esempio n. 8
0
    def testFindLoop(self):

        list = List()
        a = Node('A')
        b = Node('B')
        c = Node('C')
        d = Node('D')
        e = Node('E')

        list.head = a
        a.next = b
        b.next = c
        c.next = d
        d.next = e
        e.next = c

        self.assertEqual(findLoop(list), c)
Esempio n. 9
0
 def test_insert_index(self):
     """测试插入(通过下标)节点"""
     link = LinkedList(head)
     node1 = Node(2)
     node2 = Node(3)
     node3 = Node(4)
     link.head.next = node1
     node1.next = node2
     node2.next = node3
     link.insert_by_index(2, 1)
     self.assertTrue(link.get_size() == 5)
     self.assertTrue(link.get_data(1) == 2)
     self.assertTrue(link.get_data(2) == 2)
     link.insert_by_index(2, -1)
     link.insert_by_index(2, 100)
     link.insert_by_index(2, 'ddddw')
     self.assertTrue(link.get_size() == 5)
Esempio n. 10
0
def pad_zero(n, num):
    new_head = n
    next_node = n
    for _ in range(num):
        new_head = Node(0)
        new_head.next = next_node
        next_node = new_head
    return new_head
Esempio n. 11
0
        def testFindLoop(self):

                list = List()
                a = Node('A')
                b = Node('B')
                c = Node('C')
                d = Node('D')
                e = Node('E')

                list.head = a
                a.next = b
                b.next = c
                c.next = d
                d.next = e
                e.next = c

                self.assertEqual(findLoop(list), c)
Esempio n. 12
0
def append_to_node(data, n: LinkedList.Node, val=None):
    if type(data) == int:
        n.val = data
        return n.next
    elif type(data) == LinkedList.Node:
        n.val = val
        n.next = data if data.val else None
        return data
    return None
Esempio n. 13
0
 def push(self, v):
     temp = Node(v)
     if self.top:
         temp.next = self.top
         self.top = temp
     else:
         self.top = temp
         self.bottom = temp
     self.length += 1
def naiveFillSmallLinkedList(val, nodeList, node):
    if node is None:
        node = Node(val)
        nodeList.head = node
    else:
        temp = Node(val)
        node.next = temp
        node = node.next
    return node, nodeList
def reverseLinkedList(head):
    current = head
    prev = Node(None)
    prev.next = current
    while current:
        head = head.next
        current.next = prev
        prev = current
        current = head
    return prev
Esempio n. 16
0
def deleteMiddleNode(node: ll.Node):

    #grab data of next node in the list
    #head -> targetnode -> (node2) -> tail
    nextNode = node.next

    #set data of current node to next node
    #head -> (targetnode.data = node2.data) -> node2 -> tail
    node.data = nextNode.data

    #set current node next to node after next
    #head -> targetnode -> tail
    #(node2) -> tail
    nextNextNode = nextNode.next

    if (nextNextNode is None):
        node.next = None
    else:
        node.next = nextNextNode
 def InsertAtEnd(self, data):
     if self.head == None:
         print("Empty linked list.")
     else:
         ptr = self.head
         while ptr.next != self.head:
             ptr = ptr.next
         new = Node(data)
         ptr.next = new
         new.next = self.head
Esempio n. 18
0
def add_lists_helper(n1, n2):
    if n1 is None and n2 is None:
        return None, 0

    n, carry = add_lists_helper(n1.next, n2.next)
    val = carry + n1.data + n2.data
    new = Node(val % 10)
    new.next = n

    return new, 1 if val >= 10 else 0
Esempio n. 19
0
def main():
    one = Node(2)
    two = Node(3)
    three = Node(4)
    one.next = two
    list_one = CustomLinkedList()
    list_one.insert_at_the_beginning(one)
    list_one.insert_at_the_beginning(two)
    list_one.insert_at_the_beginning(three)
    four = find_start_of_loop(list_one)
 def InsertAtBeginning(self, data):
     if self.head == None:
         print("EMpty linked list.")
     else:
         new = Node(data)
         ptr = self.head
         while ptr.next != self.head:
             ptr = ptr.next
         ptr.next = new
         new.next = self.head
         self.head = new
Esempio n. 21
0
def reverse(head):
    temp = None
    while head:
        newnode = Node(head.data)
        if temp:
            newnode.next = temp
            temp = newnode
        else:
            temp = newnode
        head = head.next
    return temp
Esempio n. 22
0
def add_numbers_using_linked_list(node1, node2, carry):
    if node1 is None and node2 is None:
        return
    result = Node(carry)
    value = carry
    value = value + node1.content + node2.content
    result.content = value % 10
    nextNode = add_numbers_using_linked_list(
        None if node1 is None else node1.next,
        None if node2 is None else node2.next, 1 if value > 10 else 1)
    result.next = nextNode
    return result
 def insert(self, val):
     """
     inserts data in order.  This would be a sorting algorithm of Complexity o(n)
     :param val:
     :return:
     """
     current = self.head
     new_node = Node(val)
     if current is None:
         self.head = new_node
         return
     elif current.val > val:
         new_node.next = current
         self.head = new_node
         return
     while current.next is not None:
         if current.next.val > val:
             break
         current = current.next
     new_node.next = current.next
     current.next = new_node
Esempio n. 24
0
def delete_middle_node(n: Node) -> None:
    """
  Imagine: a -> b -> c -> d -> e, and we're given c, told to remove it.
  Idea: if we wanted to remove c cleanly, we would be given access to b. But we aren't.
  To work around this fact, we copy d's value <- c, then b -> d -> d -> e, then we just have to 
  remove the second d. 

  Super weird problem. But also just two lines of Python. Ha!
  """
    n.data = n.next.data
    n.next = n.next.next
    return None
Esempio n. 25
0
def reverse(curr: LinkedList.Node) -> LinkedList.Node:
    if not curr.next:
        return curr
    temp1 = curr.next
    temp2 = temp1.next
    curr.next = None
    while temp2:
        temp1.next = curr
        curr = temp1
        temp1 = temp2
        temp2 = temp2.next
    temp1.next = curr
    return temp1
Esempio n. 26
0
    def insertionSortList(self, A):

        fake_head = Node(None)
        fake_head.next = A

        prev, tmp = fake_head, A

        while tmp:
            prev.next = tmp.next
            prev = self.insert(tmp, fake_head, prev)
            tmp = prev.next

        return fake_head.next
Esempio n. 27
0
def swapPairs(head: Node) -> Node:
    root = prev = Node(None)
    prev.next = head
    while head and head.next:
        b = head.next
        head.next = b.next
        b.next = head

        prev.next = b

        head = head.next
        prev = prev.next.next
    return root.next
Esempio n. 28
0
def sumIt(n1, n2, carry):
  if (n1 is None) and (n2 is None):
    return None
  result = Node()
  v = carry
  if n1 is not None:
    v += n1.value
  if n2 is not None:
    v += n2.value
  result.value = v%10
  next = sumIt(n1.next, n2.next, v/10)
  result.next = next
  return result
def reverseSubList(head,start,finish):
    sublistHead= head
    prev = Node(None)
    prev.next = sublistHead
    for _ in range(1,start):
        sublistHead = sublistHead.next
        prev = prev.next
    
    sublistHead = sublistHead.next
    prev1 = prev
    prev = prev.next
    current = sublistHead
    prev.next = current
    for _ in range(finish-start):
        sublistHead = sublistHead.next
        current.next = prev
        prev = current
        current = sublistHead
    
    temp = prev1.next
    temp.next = sublistHead
    prev1.next = prev
    return head
Esempio n. 30
0
def add_lists_from_front_recursively(n1, n2):
    len1 = length(n1)
    len2 = length(n2)
    if len1 > len2:
        n2 = pad_zero(n2, len1-len2)
    else:
        n1 = pad_zero(n1, len2-len1)

    result, carry = add_lists_helper(n1, n2)

    if carry > 0:
        n = Node(carry)
        n.next = result
        return n
    return result
Esempio n. 31
0
def addSameSize(head1, head2, carry):
    #check if the lists are empty
    if head1 is None:
        return None

    result = Node()

    result.next = addSameSize(head1.next, head2.next, carry)

    sum = head1.data + head2.data + carry
    carry = sum / 10
    sum = sum % 10

    result.data = sum

    return result
Esempio n. 32
0
def removeDuplicate(node: Node):
    """
    using Hash Table
    O(n) time
    O(n) space
    """
    if node is None:
        return

    hash_table = dict([(node.data, True)])
    while node.next is not None:
        if node.next.data in hash_table:
            node.next = node.next.next
        else:
            hash_table[node.next.data] = True
            node = node.next
Esempio n. 33
0
def add_two_list_recursive(list_1,list_2,carry=0):
    if not list_1 and not list_2:
        if carry > 0:
            return Node(carry)
        return None
    else:
        value = carry
        result_node = Node()
        if list_1:
            value += list_1.data
        if list_2:
            value += list_2.data
        if value >= 10:
            carry = 1
        else:
            carry = 0
        result_node.data = value % 10
        next_node = add_two_list_recursive(list_1.next,list_2.next,carry)
        result_node.next = next_node
        return result_node
Esempio n. 34
0
def rotate_llist(head, k):
    dummy = Node(-1, next=head)
    # count how many nodes in the list
    len = 1
    last = head # find the last node
    while last.next:
        len += 1
        last = last.next
    k = k % len
    if k == 0: return dummy.next
    
    # reset
    prev = dummy
    head = dummy.next
    # point to the new head and the previous node
    for _ in range(len-k):
        prev = head
        head = prev.next
    prev.next = last.next
    last.next = dummy.next
    dummy.next = head
    return dummy.next
Esempio n. 35
0
      kthArray.insert(0, node.data)
      kthArray.pop(k)
    else:
      kthArray.insert(0, node.data)

    node = node.next

  print kthArray
  if len(kthArray) != k:
    print "List too short!!"
    return None

  return kthArray[k-1]

node1 = Node(1)
node2 = Node(2)
node1.next = node2
node3 = Node(3)
node2.next = node3
node4 = Node(4)
node3.next = node4
node5 = Node(5)
node4.next = node5
node6 = Node(6)
node5.next = node6
node7 = Node(7)
node6.next = node7

print_list(node1)
print findKthLastNode(node1, 4)
Esempio n. 36
0
  def test_find_loop_node(self):
    n1 = Node(1)
    n2 = Node(2)
    n3 = Node(3)
    n4 = Node(4)
    n5 = Node(5)
    n6 = Node(6)
    n7 = Node(7)
    n8 = Node(8)
    n9 = Node(9)
    n10 = Node(10)
    n11 = Node(11)

    l1 = LinkedList()
    l1.head = n1
    n1.next = n2
    n2.next = n3
    n3.next = n4
    n4.next = n5
    n5.next = n6
    n6.next = n7
    n7.next = n8
    n8.next = n9
    n9.next = n10
    n10.next = n11
    n11.next = n6
    actual = find_loop_node(l1)
    self.assertEqual(actual, n6)

    l1 = LinkedList()
    l1.head = n1
    n1.next = n2
    n2.next = n3
    n3.next = n4
    n4.next = n5
    n5.next = n6
    n6.next = n7
    n7.next = n8
    n8.next = n9
    n9.next = n10
    n10.next = n11
    n11.next = n8
    actual = find_loop_node(l1)
    self.assertEqual(actual, n8)
Esempio n. 37
0
        else:
            longNode = longNode.next   
            shortNode = shortNode.next

    return False

def getListLength(node):
    counter = 0
    while node:
        counter += 1
        node = node.next

    return counter

nodeOne1 = Node(7)
nodeOne2 = Node(1)
nodeOne1.next = nodeOne2
nodeOne3 = Node(6)
nodeOne2.next = nodeOne3

nodeTwo1 = Node(5)
nodeTwo2 = Node(9)
nodeTwo1.next = nodeTwo2
nodeTwo3 = Node(2)
nodeTwo2.next = nodeTwo3

nodeOne1.next = nodeTwo1


answerNode = returnIntersectingNode(nodeOne1, nodeTwo1)
print_list(answerNode)