Exemple #1
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')
t2.next_node.next_node = Node('c')
Exemple #2
0
    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')
t21.next_node.next_node.next_node = Node('a')
    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')
t21.next_node.next_node.next_node = Node('a')
 def test_creation1(self):
     """Test manual assignment of next node."""
     first = Node(100)
     second = Node(200)
     first.next_node = second
     self.assertEqual(first.next_node, second)
Exemple #5
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')
t2.next_node.next_node = Node('c')