Example #1
0
def partition(ll, x):

    head1 = LinkedListNode(0)
    head2 = LinkedListNode(0)

    temp = ll.head
    phead1 = head1
    phead2 = head2

    while temp:

        if temp.value < x:
            phead1.next = temp
            temp = temp.next
            phead1 = phead1.next
            phead1.next = None
        else:
            phead2.next = temp
            temp = temp.next
            phead2 = phead2.next
            phead2.next = None

    phead1.next = head2.next
    head = head1.next
    return head
Example #2
0
def sum_lists_followup(ll_a, ll_b):
    la = len(ll_a)
    lb = len(ll_b)
    if la > lb:
        for i in range(la - lb):
            ll_b.add_to_beginning(0)

    elif lb > la:
        for i in range(lb - la):
            ll_a.add_to_beginning(0)

    # return carry
    def dfs(ll_a, ll_b, ll_n):
        if not ll_a or not ll_b:
            return 0

        c = dfs(ll_a.next, ll_b.next, ll_n)
        v = (c + ll_a.value + ll_b.value) % 10
        c = (c + ll_a.value + ll_b.value) // 10
        n = LinkedListNode(v)
        if ll_n.head is None:
            ll_n.head = ll_n.tail = n
        else:
            n.next = ll_n.head
            ll_n.head = n
        return c

    ll = LinkedList()
    c = dfs(ll_a.head, ll_b.head, ll)
    if c:
        n = LinkedListNode(c)
        n.next = ll.head
        ll.head = n
    return ll
def partition(llist, partition):
	if llist.head is None:
		return

	current = llist.head
	leftTail = LinkedListNode(None, current)

	while current.value < partition:
		leftTail = leftTail.next
		current = current.next

	current = leftTail
	firstTime = True
	while current.next:
		if current.next.value >= partition:
			current = current.next
		else:
			if firstTime and leftTail.value:
				llist.head = leftTail
				firstTime = False
			temp = leftTail.next
			leftTail.next = current.next
			leftTail = current.next
			current.next = leftTail.next
			leftTail.next = temp

	print llist
 def reversed_list(node):
     head = None
     while node:
         n = LinkedListNode(node.val)
         n.next = head
         head = n
         node = node.next
     return head
Example #5
0
    def test_convert_to_int(self):
        head = LinkedListNode(4)
        self.assertEqual(4, head.convert_to_int())

        head.AppendToTail('8')
        self.assertEqual(84, head.convert_to_int())

        head.AppendToTail('8')
        self.assertEqual(884, head.convert_to_int())
def ReverseLL2(ll):
	current = ll.head
	head = None
	while current:
		n = LinkedListNode(current.value)
		n.next = head
		head = n
		current = current.next

	ll_clone = LinkedList()
	ll_clone.head = head	
	return (ll_clone)	
Example #7
0
    def dfs(ll_a, ll_b, ll_n):
        if not ll_a or not ll_b:
            return 0

        c = dfs(ll_a.next, ll_b.next, ll_n)
        v = (c + ll_a.value + ll_b.value) % 10
        c = (c + ll_a.value + ll_b.value) // 10
        n = LinkedListNode(v)
        if ll_n.head is None:
            ll_n.head = ll_n.tail = n
        else:
            n.next = ll_n.head
            ll_n.head = n
        return c
def partition(head, x):
    before = before_head = LinkedListNode(0)
    after = after_head = LinkedListNode(0)
    while head:
        if head.val < x:
            before.next = head
            before = before.next
        else:
            after.next = head
            after = after.next
        head = head.next
    after.next = None
    before.next = after_head.next
    return before_head.next
Example #9
0
def sum_lists(l1, l2):
    sum = sum_head = LinkedListNode(0)
    carry = 0
    while l1 or l2:
        val1 = l1.val if l1 else 0
        val2 = l2.val if l2 else 0
        digit_sum = val1 + val2 + carry
        sum.next = LinkedListNode(digit_sum % 10)
        sum = sum.next
        carry = int(math.floor(digit_sum/10))
        l1 = l1.next if l1 else None
        l2 = l2.next if l2 else None
    if carry == 1:
        sum.next = LinkedListNode(1)
    return sum_head.next
    def add(self, key, value):
        keyNode = LinkedListNode(key)
        self.valueList[key] = Record(value, keyNode)
        addingRes = self.keyList.addNodeToHead(keyNode)

        if addingRes != None:
            del self.valueList[addingRes]
Example #11
0
def q7():
	foo = LinkedList( [1,2,3] )
	bar = LinkedList( [3,3,3] )
	baz = LinkedListNode(44, None, foo.tail)

	foo.tail.next = baz
	bar.tail.next = baz
	foo.tail      = foo.tail.next
	bar.tail      = bar.tail.next
	assert bar.tail is baz

	foo.append(99)							#This should add 99 to bar, too
	baz = foo.tail
	bar.tail.next = baz					#But it will not update bar, like it will foo
	bar.tail      = bar.tail.next		#So, update bar's tail manually
	assert foo == LinkedList([1,2,3,44,99])			#This is `foo`
	assert bar == LinkedList([3,3,3,44,99])			#This is `bar`
	assert getIntersection(foo, bar) == foo.get(3)	#They share `44` first

	foo = LinkedList([1,2])
	baz = bar.get(4)
	foo.tail.next = baz
	foo.tail      = foo.tail.next
	assert getIntersection(foo, bar) == baz							#Different length
	assert getIntersection(bar,foo) == baz								#Longer first
	assert getIntersection(foo, foo) == foo.head						#Same LinkedList
	assert getIntersection(LinkedList(), LinkedList()) is None	#Same None LL
	assert getIntersection(LinkedList(), foo) is None				#No Intersection w/ None LL
	
	bar = LinkedList([1,2,3])
	assert getIntersection(bar,foo) == None							#No Intersection w/ filled LL

	print('q7.py: PASSED ALL TESTS')
	return True
def reverse_list_out_of_place(head):
    if not head:
        return None
    if not head.next:
        return head

    prev = None
    cur = head

    while cur:
        new_node = LinkedListNode(cur.value)
        new_node.next = prev

        prev = new_node
        cur = cur.next

    return prev
Example #13
0
 def insert(self, vertex1, vertex2):
     # If graph is empty
     if self.adjacency_lists.head == None:
         self.adjacency_lists.head = LinkedListNode(vertex1)
         self.adjacency_lists.head.next = LinkedListNode(vertex2)
     else:
         cur_list = self.adjacency_lists.head
         # Check if vertex1 has adjacent vertices
         while cur_list.next_list != None and cur_list.value != vertex1:
             cur_list = cur_list.next_list
         # If vertex1 has adjacent vertices
         if cur_list.value == vertex1:
             if cur_list.next == None:
                 cur_list.next = LinkedListNode(vertex2)
                 return
             cur = cur_list.next
             while cur.next != None:
                 if cur.value == vertex2:
                     raise Exception("Edge already exists")
                 cur = cur.next
             cur.next = LinkedListNode(vertex2)
             return
         # If vertex1 does not have adjacent vertices
         else:
             cur_list.next_list = LinkedListNode(vertex1)
             cur_list = cur_list.next_list
             cur_list.next = LinkedListNode(vertex2)
Example #14
0
def add_two_integers(ll1, ll2):
    if not ll1 and not ll2:
        return None
    if not ll1:
        return ll2
    if not ll2:
        return ll1

    carry = 0
    curr1 = ll1
    curr2 = ll2

    output_list_head = LinkedListNode(0)
    curr_output = output_list_head
    while curr1 or curr2 or carry:
        curr_val = carry + (curr1.val if curr1 else 0) + (curr2.val
                                                          if curr2 else 0)
        curr_output.next = LinkedListNode(curr_val % 10)
        carry = int(curr_val / 10)
        curr1 = curr1.next if curr1 else None
        curr2 = curr2.next if curr2 else None
        curr_output = curr_output.next

    return output_list_head.next
Example #15
0
    def test_addition(self):
        head = LinkedListNode(7)
        self.assertEqual(head.addition(head), LinkedListNode(4).AppendToTail(1))

        head = LinkedListNode(21)
        foo = head.addition(LinkedListNode(42))
        bar = LinkedListNode(3).AppendToTail(6)
        self.assertEqual(foo, bar)

        head = LinkedListNode(3)
        head = head.AppendToTail(1)
        head = head.AppendToTail(5)
        other = LinkedListNode(5)
        other = other.AppendToTail(9)
        other = other.AppendToTail(2)
        self.assertEqual(head.addition(other), LinkedListNode(8).AppendToTail(0).AppendToTail(8))
Example #16
0
def sum_lists(ll1, ll2):

    head1, head2 = ll1.head, ll2.head
    result = LinkedList()
    carry = 0

    while head1 or head2:
        temp = carry

        if head1:
            temp += head1.value
            head1 = head1.next
        if head2:
            temp += head2.value
            head2 = head2.next

        carry = temp // 10
        left = temp % 10
        node = LinkedListNode(left)
        result.add(node)

    return result
Example #17
0
class TestLinkedList(unittest.TestCase):
	foo = LinkedList()
	bar = LinkedListNode(6)
	foo.set_head(bar)
	foo.add(LinkedListNode(6))
	foo.add(LinkedListNode(5))
	foo.add(LinkedListNode(5))
	foo.add(LinkedListNode(5))
	foo.add(LinkedListNode(5))
	foo.add(LinkedListNode(7))
	foo.add(LinkedListNode(8))
	foo.add(LinkedListNode(5))
	foo.add(LinkedListNode(5))
	foo.add(LinkedListNode(5))
	foo.add(LinkedListNode(5))
	def test_remove_duplicates(self):
		test = 
from LinkedList import LinkedListNode
from LinkedList import LinkedList

head = LinkedListNode('Angel Food')
b = LinkedListNode('Bundt')
c = LinkedListNode('Cheese')
d = LinkedListNode('Devils Food')
e = LinkedListNode('Eccles')

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

def kth_to_last_node(k, head):
    count = 0
    node = head

    while node:
        node = node.next
        count += 1

    if k > count:
        raise Exception('k is larger than the list size')

    stop_at = count - k

    node = head
    i = 0
    while i < stop_at:
        node = node.next
def step_impl(context):
    """
	:type context: behave.runner.Context
	"""
    context.node = LinkedListNode(5)
    pass
def step_impl(context):
    """
	:type context: behave.runner.Context
	"""
    context.head.next = LinkedListNode(13)
    pass
    prev = None
    cur = head

    while cur:
        new_node = LinkedListNode(cur.value)
        new_node.next = prev

        prev = new_node
        cur = cur.next

    return prev



head = LinkedListNode(1)
head.next = LinkedListNode(2)
head.next.next = LinkedListNode(3)
head.next.next.next = LinkedListNode(4)

li = LinkedList()
li.root = head
print li

new_head = reverse_list_inplace(head)
li.root = new_head
print li

new_new_head = reverse_list_out_of_place(new_head)
li_new = LinkedList()
li_new.root = new_new_head
Example #22
0
    while runner:
        record[runner.value] = runner
        runner = runner.next

    runner = ll2.head

    while runner:
        if runner.value in record and record[runner.value] is runner:
            return runner
        runner = runner.next

    return False


if __name__ == '__main__':
    n3 = LinkedListNode(3)
    n2 = LinkedListNode(2, n3)
    n1o1 = LinkedListNode(1, n2)
    n1o2 = LinkedListNode(5, n2)

    ll1 = LinkedList()
    ll1.head = n1o1
    ll1.tail = n3

    ll2 = LinkedList()
    ll2.head = n1o2
    ll2.tail = n3

    print(ll1)
    print(ll2)
Example #23
0
 def Push(self, data):
     new_node = LinkedListNode(data)
     new_node.next = self.top
     self.top = new_node
Example #24
0
 def deleteMiddleNode(self, node: LinkedListNode):
     # Reference next node to current node
     node.value = node.next.value
     node.next = node.next.next
Example #25
0
#!/usr/bin/env python
"""
Given a sorted linked list, delete all duplicates such that each element appears
only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->, return 1->2->3.
"""

from LinkedList import LinkedListNode

head = LinkedListNode(1)
head.next = LinkedListNode(1)
head.next.next = LinkedListNode(2)

# @params {LinkedListNode} head of a linked list


def remove_duplicates(head):
    if not head or not head.next:
        return head

    slow_pointer = head  #assign head to slow pointer vatriable
    fast_pointer = head.next  #assign next from head to fast_pointer variable

    while fast_pointer:
        if slow_pointer.data == fast_pointer.data:  #if head and next matches
            slow_pointer.data = fast_pointer.data
            fast_pointer = fast_pointer.next
        else:
            slow_pointer = slow_pointer.next
Example #26
0
    output_list_head = LinkedListNode(0)
    curr_output = output_list_head
    while curr1 or curr2 or carry:
        curr_val = carry + (curr1.val if curr1 else 0) + (curr2.val
                                                          if curr2 else 0)
        curr_output.next = LinkedListNode(curr_val % 10)
        carry = int(curr_val / 10)
        curr1 = curr1.next if curr1 else None
        curr2 = curr2.next if curr2 else None
        curr_output = curr_output.next

    return output_list_head.next


n1 = LinkedListNode(1)
n2 = LinkedListNode(0)
n3 = LinkedListNode(9)
n4 = LinkedListNode(9)
n5 = LinkedListNode(9)
n6 = LinkedListNode(9)

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

print(LinkedList(n1))

m1 = LinkedListNode(7)