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
Beispiel #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 reversed_list(node):
     head = None
     while node:
         n = LinkedListNode(node.val)
         n.next = head
         head = n
         node = node.next
     return head
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)	
Beispiel #5
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 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
    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
print li_new
Beispiel #8
0
 def Push(self, data):
     new_node = LinkedListNode(data)
     new_node.next = self.top
     self.top = new_node
Beispiel #9
0
 def deleteMiddleNode(self, node: LinkedListNode):
     # Reference next node to current node
     node.value = node.next.value
     node.next = node.next.next
Beispiel #10
0
        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)
m2 = LinkedListNode(3)
m3 = LinkedListNode(2)

m1.next = m2
m2.next = m3

print(LinkedList(m1))
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
Beispiel #12
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