Пример #1
0
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        str1 = ""
        str2 = ""

        while l1:
            str1 += str(l1.data)
            l1 = l1.nextnode

        while l2:
            str2 += str(l2.data)
            l2 = l2.nextnode

        str1 = int(str1)
        str2 = int(str2)

        add_sum = str1 + str2
        add_sum = str(add_sum)

        new_head = Node(add_sum[0])
        current = new_head

        for ch in add_sum[1:len(add_sum)]:
            current.nextnode = Node(ch)
            current = current.nextnode

        new_list = LinkedList()
        new_list.head = new_head

        print new_list.traverseList()
Пример #2
0
    def sortlist(self, my_arr):

        my_list = LinkedList()

        self.head = Node(my_arr[0])
        my_list.head = self.head
        current = self.head

        for i in range(1, len(my_arr)):
            my_arr[i] = Node(my_arr[i])
            # thisnode = Node(my_arr[i])
            current.nextnode = my_arr[i]
            current = my_arr[i]

        print my_list.traverseList()
m = Node(8)
n = Node(9)
o = Node(7)
p = Node(25)
# q = Node(4)
# r = Node(5)
# s = Node(6)

m.nextnode = n
n.nextnode = o
o.nextnode = p
# p.nextnode = q
# q.nextnode = r
# r.nextnode = s

first_list = LinkedList()
first_list.head = a

second_list = LinkedList()
second_list.head = m

print "*** Ans ***\n"


def intersection(one_list, two_list):
    x = one_list.size()
    y = two_list.size()

    if x > y:
        first = x - y
        first_head = one_list.head
# c = Node(6)
# d = Node(3)
# e = Node(4)
# f = Node(5)
# g = Node(6)


a.nextnode = b
# b.nextnode = c
# c.nextnode = d
# d.nextnode = e
# e.nextnode = f
# f.nextnode = g


first_list = LinkedList()
first_list.head = a

class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """

        current = head.nextnode
        prev =head

        while current:
            if prev.data == val:
from Linked_Lists.Single_ll.Node import Node
from Linked_Lists.Single_ll.LinkedList import LinkedList

a = Node(7)
b = Node(5)
c = Node(7)

a.nextnode = b
b.nextnode = c

my_list = LinkedList()
my_list.head = a

my_list.traverseList()

print "-----"


def is_palindrome(my_list):
    if my_list.head is None:
        return True

    if my_list.head.nextnode is None:
        return True

    current_node = my_list.head
    my_str = ""
    while current_node is not None:
        my_str = my_str + str(current_node.data)
        current_node = current_node.nextnode
Пример #6
0
d = Node(7)

a.nextnode = b
b.nextnode = c
c.nextnode = d

m = Node(2)
n = Node(4)
o = Node(6)
p = Node(8)

m.nextnode = n
n.nextnode = o
o.nextnode = p

first_list = LinkedList()
first_list.head = a

second_list = LinkedList()
second_list.head = m

print "*** Ans ***\n"

# l1 and l2 are head


def merge(l1, l2):
    if not l1 or not l2:
        return l1 or l2
    if l1.data < l2.data:
        l1.nextnode = merge(l1.nextnode, l2)
Пример #7
0
from Linked_Lists.Single_ll.Node import Node
from Linked_Lists.Single_ll.LinkedList import LinkedList

a = Node(7)
b = Node(5)
c = Node(7)
d = Node(4)
e = Node(1)

a.nextnode = b
b.nextnode = c
c.nextnode = d
d.nextnode = a

my_list = LinkedList()
my_list.head = a

# my_list.traverseList()

print "-----"


def cycle_detect(my_list):
    try:
        first = my_list.head
        second = my_list.head.nextnode

        while first is not second:
            first = first.nextnode
            second = second.nextnode.nextnode
        print first.data