Exemplo 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()
Exemplo n.º 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()
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:
                head = current
                prev = current
                current = current.nextnode
                # prev.nextnode = current
            elif current.data == val:
                current = current.nextnode
                prev.nextnode = current
            else:
                prev = current
                current = current.nextnode

        return head


my_obj = Solution()
my_obj.removeElements(a,1)
print first_list.traverseList()
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