Esempio n. 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()
from Linked_Lists.Single_ll.Node import Node
from Linked_Lists.Single_ll.LinkedList import LinkedList

a = Node(1)
b = Node(2)
c = Node(3)
d = Node(25)

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

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
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None


from Linked_Lists.Single_ll.Node import Node
from Linked_Lists.Single_ll.LinkedList import LinkedList

a = Node(1)
b = Node(1)
# 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):
from Linked_Lists.Single_ll.Node import Node
from Linked_Lists.Single_ll.LinkedList import LinkedList

a = Node(2)
b = Node(6)
c = Node(9)
d = Node(11)
e = Node(16)

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

first_list = LinkedList()
first_list.head = a


class Solution(object):
    def oddEvenList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None
        odd = head
        even = head.nextnode
        while even and even.nextnode is not None:
            temp = even.nextnode
            even.nextnode = even.nextnode.nextnode
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
from Linked_Lists.Single_ll.Node import Node
from Linked_Lists.Single_ll.LinkedList import LinkedList

a = Node(1)
b = Node(2)
c = Node(0)
d = Node(1)
e = Node(0)

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

my_list = LinkedList()
my_list.head = e
my_list.traverseList()
print "\n-------------------------"

def sort_list(my_node):
    current_node = my_node

    count = [0,0,0]
    while current_node:
        count[current_node.data] += 1
        current_node = current_node.nextnode

    i = 0
    current_node = my_node

    while current_node:
Esempio n. 7
0
from Linked_Lists.Single_ll.Node import Node
from Linked_Lists.Single_ll.LinkedList import LinkedList

a = Node(1)
b = Node(3)
c = Node(5)
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
Esempio n. 8
0
from Linked_Lists.Single_ll.Node import Node
from Linked_Lists.Single_ll.LinkedList import LinkedList

a = Node(7)
b = Node(2)
c = Node(4)
d = Node(3)

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

m = Node(5)
n = Node(6)
o = Node(4)

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

from Linked_Lists.Single_ll.Node import Node
from Linked_Lists.Single_ll.LinkedList import LinkedList

a = Node(5)
b = Node(4)
c = Node(1)

a.nextnode = b
b.nextnode = c

my_list = LinkedList()
my_list.head = a

# for i in range(1, len(my_arr)):
#     current_val = my_arr[i]
#     position = i
#
#     while position > 0 and my_arr[position - 1] > current_val:
#         my_arr[position] = my_arr[position - 1]
#         position -= 1
#
#     my_arr[position] = current_val



class Solution(object):
    def insertionSortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
Esempio n. 10
0
from Linked_Lists.Single_ll.Node import Node
from Linked_Lists.Single_ll.LinkedList import LinkedList

a = Node(8)
b = Node(7)
c = Node(6)
d = Node(5)
e = Node(2)

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

my_list = LinkedList()
my_list.head = e

my_list.traverseList()

print "-----------------------------------------"
print "-----------------------------------------"


def my_insert(self, my_node):
    current = self.head
    my_temp_head = self.head

    if my_node.data < current.data:
        my_node.nextnode = current
        self.head = my_node
Esempio n. 11
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