コード例 #1
0
 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
コード例 #2
0
 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)
コード例 #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
コード例 #4
0
    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)
コード例 #5
0
 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)
コード例 #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
コード例 #7
0
 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)
コード例 #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')
コード例 #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')
コード例 #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')
コード例 #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)
コード例 #12
0
ファイル: 2_2.py プロジェクト: Cyrus-Xi/Programming-Practice
    # 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')
コード例 #13
0
ファイル: 2_1.py プロジェクト: Cyrus-Xi/Programming-Practice
    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')
コード例 #14
0
ファイル: 2_3.py プロジェクト: adusca/SmallAlgos
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)
コード例 #15
0
ファイル: test.py プロジェクト: jbbarrau/problems
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()
コード例 #16
0
ファイル: linkedlistorderadd.py プロジェクト: nirmalvp/algos
def insertFirst(theList,data):
	node = Node(data)
	node.next = theList.first
	theList.first = node
コード例 #17
0
ファイル: 2_8.py プロジェクト: adusca/SmallAlgos
    # 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)