示例#1
0
    def test_it3(self):
        node1 = LinkedListNode(1)
        node2 = LinkedListNode(2)
        node3 = LinkedListNode(3)
        node4 = LinkedListNode(4)
        node5 = LinkedListNode(1)

        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node5

        assert not is_circulation(node1)
示例#2
0
    def test_memorize_nodes(self):
        node1 = LinkedListNode(1)
        node2 = LinkedListNode(2)
        node3 = LinkedListNode(3)
        node4 = LinkedListNode(2)
        node5 = LinkedListNode(1)

        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node5

        actual = memorize_nodes(node1)
        expect = {1: [node1, node5], 2: [node2, node4], 3: [node3]}
        assert actual == expect
def linked_list_add_forward(linked_list_one, linked_list_two, carry):
    # convert the first list to an integer
    current_node = linked_list_one
    first_integer_value = 0
    while current_node:
        first_integer_value = first_integer_value * 10 + current_node.data
        current_node = current_node.next

    current_node = linked_list_two
    second_integer_value = 0
    while current_node:
        second_integer_value = second_integer_value * 10 + current_node.data
        current_node = current_node.next

    result = first_integer_value + second_integer_value

    # convert result back to a linked list
    divided_by_ten = result
    next_node = None
    while divided_by_ten != 0:
        current_digit = divided_by_ten % 10
        divided_by_ten = divided_by_ten / 10
        new_node = LinkedListNode(current_digit)
        new_node.next = next_node
        next_node = new_node

    return new_node
def linked_list_add(first_linked_list, second_linked_list, carry):
    """
    The numbers are stored in reverse order
    """
    if not first_linked_list and not second_linked_list and carry == 0:
        return None

    value = carry
    if first_linked_list:
        value = value + first_linked_list.data

    if second_linked_list:
        value = value + second_linked_list.data

    result = value % 10 # the last digit
    result_linked_list = LinkedListNode(result)

    if first_linked_list or second_linked_list:
        next_node = linked_list_add(None if not first_linked_list else first_linked_list.next,
                                    None if not second_linked_list else second_linked_list.next,
                                    0 if value < 10 else 1)

        result_linked_list.next = next_node

    return result_linked_list
def linked_list_add_forward(linked_list_one, linked_list_two, carry):
    # convert the first list to an integer
    current_node = linked_list_one
    first_integer_value = 0
    while current_node:
        first_integer_value = first_integer_value * 10 + current_node.data
        current_node = current_node.next

    current_node = linked_list_two
    second_integer_value = 0
    while current_node:
        second_integer_value = second_integer_value * 10 + current_node.data
        current_node = current_node.next

    result = first_integer_value + second_integer_value

    # convert result back to a linked list
    divided_by_ten = result
    next_node = None
    while divided_by_ten != 0:
        current_digit = divided_by_ten % 10
        divided_by_ten = divided_by_ten / 10
        new_node = LinkedListNode(current_digit)
        new_node.next = next_node
        next_node = new_node

    return new_node
def linked_list_add(first_linked_list, second_linked_list, carry):
    """
    The numbers are stored in reverse order
    """
    if not first_linked_list and not second_linked_list and carry == 0:
        return None

    value = carry
    if first_linked_list:
        value = value + first_linked_list.data

    if second_linked_list:
        value = value + second_linked_list.data

    result = value % 10  # the last digit
    result_linked_list = LinkedListNode(result)

    if first_linked_list or second_linked_list:
        next_node = linked_list_add(None if not first_linked_list else first_linked_list.next,
                                    None if not second_linked_list else second_linked_list.next,
                                    0 if value < 10 else 1)

        result_linked_list.next = next_node

    return result_linked_list
示例#7
0
    def test_it(self):
        node1 = LinkedListNode(1)
        node2 = LinkedListNode(2)
        node3 = LinkedListNode(3)
        node4 = LinkedListNode(4)
        node5 = LinkedListNode(5)

        # List1, node1 -> 2 -> 3
        node1.next = node2
        node2.next = node3

        # List2, node5 -> 4 -> 3
        node5.next = node4
        node4.next = node3

        assert extract_common_node(node1, node5) == [node3]
 def push(self, value):
     """Method for adding a node to the list"""
     node = LinkedListNode(value)
     if self.is_empty():
         self.head = node
     else:
         node.next = self.head
         self.head = node
示例#9
0

def reverse(node):
    if node == None:
        return None
    if node.next == None:
        return node
    prev_node = None
    current = node
    while current:
        temp = current.next
        current.next = prev_node
        prev_node = current
        current = temp
    return (prev_node)


def display_all_values(node):
    while True:
        print(node.value)
        if node.next == None:
            break
        node = node.next


a = LinkedListNode(1)
a.next = LinkedListNode(2)
a.next.next = LinkedListNode(3)
a.next.next.next = LinkedListNode(4)
display_all_values(a)
display_all_values(reverse(a))
"""
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 linked_list import LinkedListNode

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


# @param {LinkedListNode} head head of a linked list
def remove_duplicates(head):
    if not head or not head.next:
        return head

    slow_pointer = head
    fast_pointer = head.next
    while fast_pointer:
        if slow_pointer.data == fast_pointer.data:
            slow_pointer.next = fast_pointer.next
            fast_pointer = fast_pointer.next
        else:
            slow_pointer = slow_pointer.next
            fast_pointer = fast_pointer.next

    return head
"""
Write a function to delete a node (except the tail) in a singly linked list,
given only access to that node.

Suppose the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node 
with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

https://leetcode.com/problems/delete-node-in-a-linked-list/
"""

from linked_list import LinkedListNode


# @param head LinkedListNode the head of a linked list
# @param node LinkedListNode the linked list node to be deleted
def delete_node(node):
    if node and node.next:
        node.data = node.next.data
        node.next = node.next.next


if __name__ == '__main__':
    head = LinkedListNode(1)
    head.next = LinkedListNode(2)
    head.next.next = LinkedListNode(3)
    head.next.next.next = LinkedListNode(4)

    delete_node(head.next.next)
    assert str(head) == "1, 2, 4"
    print("All test cases passed.")
"""
Write a program that reverses a linked list without using more than O(1) storage
"""
from linked_list import LinkedListNode
import random

head = LinkedListNode(random.randint(0, 20))
previous = head
for i in range(0, 5):
    node = LinkedListNode(random.randint(0, 20))
    previous.next = node
    previous = node

node.next = None

print(head)

current = head
next = current.next
current.next = None
while next:
    old_next = next.next
    next.next = current
    current = next
    next = old_next

print(current)