def append(self, item, number_of_columns=0):
     """Insert the given item at the tail of this linked list.
     TODO: Running time: O(1) Why and under what conditions?"""
     # TODO: Create new node to hold given item
     node = Node(item)
     if type(item) is list:
         node.data = LinkedList(node.data)
         # TODO: Append node after tail, if it exists
         if self.tail is None:
             self.head = node
             self.tail = node
         else:
             self.tail.next = node
             self.tail = node
     else:
         # start from first column
         columns = 0
         for node in self:
             if columns == number_of_columns:
                 linkedlist = node.data
                 linkedlist.append(item)
                 break
             else:
                 # count columns
                 columns += 1
 def test_get_at(self):
     n = Node(1)
     ll = Linkedlist()
     ll.insert_at(0, n)
     self.assertEqual(ll.get_at(0), n)
     n = Node(2)
     ll.insert_at(10, n)
     self.assertEqual(ll.get_at(1), n)
Exemple #3
0
    def setUp(self):
        self.head = Node(1)
        self.head.next = Node(3)
        self.head.next.next = Node(1)

        self.head2 = Node(0)

        self.head3 = None
    def Push(self, val):

        if self.head == None:
            self.head = Node(val, None)
        else:
            cur = self.head
            while cur.next != None:
                cur = cur.next
            cur.next = Node(val, None)
 def test_remove_at(self):
     n1 = Node(1)
     ll = Linkedlist()
     self.assertEqual(ll.remove_at(0), None)
     ll.insert_at(0, n1)
     n2 = Node(2)
     ll.insert_at(10, n2)
     n3 = Node(3)
     ll.insert_at(1, n3)
     self.assertEqual(ll.remove_at(2), n3)
     self.assertEqual(ll.remove_at(0), n1)
Exemple #6
0
def add(linked1, linked2):
    c = 0
    head = last = Node(0)
    for a, b in zip_longest(linked1, linked2, fillvalue=0):
        c += a + b
        last.next = Node(c % 10)
        last = last.next
        c //= 10
    if c:
        last.next = Node(c)
    return head.next
 def test_remove_last(self):
     n = Node(1)
     ll = Linkedlist()
     self.assertEqual(ll.remove_last(), None)
     ll.insert_last(n)
     self.assertEqual(ll.remove_last(), n)
     self.assertEqual(ll.size, 0)
     n = Node(2)
     ll.insert_last(n)
     n = Node(3)
     ll.insert_last(n)
     self.assertEqual(ll.remove_last(), n)
Exemple #8
0
 def test_prepend_many(self):
     head = Node(5)
     linked = LinkedList(head)
     linked.prepend(7)
     linked.prepend(9)
     linked.prepend(11)
     self.assertEqual(str(linked), '11->9->7->5', 'should be 11->9->7->5')
Exemple #9
0
 def test_append_many(self):
     head = Node(5)
     linked = LinkedList(head)
     linked.append(7)
     linked.append(9)
     linked.append(11)
     self.assertEqual(str(linked), '5->7->9->11', 'should be 5->7->9->11')
Exemple #10
0
 def test_remove_not_exisiting(self):
     head = Node(5)
     linked = LinkedList(head)
     linked.append(9)
     node = linked.remove(7)
     self.assertEqual(node, None, 'should be None')
     self.assertEqual(str(linked), '5->9', 'should be 5->9')
Exemple #11
0
 def tranverse(node):
     nonlocal rst #capture the variable rst
     if node is None:
         return 
     tranverse(node.right)
     rst = Node(node.data, rst) #append the result
     tranverse(node.left)
Exemple #12
0
    # Separate the pointers by k-1.
    for i in xrange(k-1):
        runner = runner.next_node
        # Went off the edge: list isn't long enough.
        if not runner: return -1
    
    # When runner goes off the end, we know curr is the kth node from the end.
    while runner:
        curr = curr.next_node
        runner = runner.next_node

    return curr.data


t1 = Node('a')
t1.next_node = Node('b')
t1.next_node.next_node = Node('c')

t1b = Node('a')
t1b.next_node = Node('b')
t1b.next_node.next_node = Node('c')

print kth_to_last(t1, 2)
print "should be b"

print kth_to_last(t1b, 2)
print "one pass, should be b"

t2 = Node('a')
t2.next_node = Node('b')
Exemple #13
0
    if not head.next_node: return head

    curr, runner = head, head.next_node

    while curr:
        runner = curr
        while runner.next_node:
            if curr.data == runner.next_node.data:
                runner.next_node = runner.next_node.next_node
            else:    
                runner = runner.next_node
        curr = curr.next_node
            

# a, a, b | a, b, b, a
t1 = Node('a')
t1.next_node = Node('a')
t1.next_node.next_node = Node('b')

t11 = Node('a')
t11.next_node = Node('a')
t11.next_node.next_node = Node('b')

t2 = Node('a')
t2.next_node = Node('b')
t2.next_node.next_node = Node('b')
t2.next_node.next_node.next_node = Node('a')

t21 = Node('a')
t21.next_node = Node('b')
t21.next_node.next_node = Node('b')
Exemple #14
0
from linkedlist import Node, print_list_from_node


def remove_middle(node):
    node.value = node.next.value
    node.next = node.next.next


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

print_list_from_node(node1)
remove_middle(node3)
print_list_from_node(node1)
Exemple #15
0
from linkedlist import Node

linkedlist = Node(1,Node(2,Node(3,Node(4,None))))
linkedlist = Node(1,Node(2,Node(3,Node(1,None))))

print linkedlist.isCircular()
Exemple #16
0
def insertFirst(theList,data):
	node = Node(data)
	node.next = theList.first
	theList.first = node
Exemple #17
0
    # Let k be the size of the cycle. Let the hare be exactly k steps
    # in front of the tortoise, and move both one step at a time. When
    # the tortoise gets to the first node in the cycle the hare will
    # also be there because N + k = N for every N in the
    # cycle. Since they cannot meet before the cycle, the first place
    # they meet is the beginning of the cycle
    tortoise = node
    hare = node
    for i in range(size):
        hare = hare.next
    while True:
        if hare == tortoise:
            return hare
        hare = hare.next
        tortoise = tortoise.next

n1 = Node(1)
n2 = Node(2)
n3 = Node(3)
n4 = Node(4)
n5 = Node(5)

n1.next = n2
n2.next = n3
n3.next = n4
n4.next = n5
n5.next = n3

print find_first_in_loop(n3)