def is_palindrome(head):
     """
     :type head: ListNode
     :rtype: bool
     """
     is_palindrome_list = LinkedList(head)
     second_half_list = LinkedList(is_palindrome_list.find_mid_node())
     reversed_second_half_list = LinkedList(second_half_list.reverse_list())
     print(Solution().compare_two_list(reversed_second_half_list, is_palindrome_list))
    def test_iterator(self):
        lst = LinkedList()
        lst.add_list(10, 23, 34, 45, 56)

        empty_list = []

        for x in lst:
            empty_list.append(str(x))
        result = " ".join(empty_list)
        assert(result == "10 23 34 45 56")
 def levelOrder(self):
     queue = LinkedList()
     queue.addFirst(self.root)
     while not queue.isEmpty():
         cur = queue.removeFirst()
         print(cur.key)
         if(cur.left != None):
             queue.addLast(cur.left)
         if(cur.right != None):
             queue.addLast(cur.right)
    def test_get(self):
        lst = LinkedList()
        lst.add(5)
        lst.add(3)
        lst.add(11)

        assert(lst.get(0) == 5)
        assert(lst.get(-1) == 11)
        assert(lst.get(2) == 11)
예제 #5
0
class LinkedListStack(Stack):
    __list = []

    def __init__(self):
        self.__list = LinkedList()

    def getSize(self):
        return self.__list.getSize()

    def isEmpty(self):
        return self.__list.isEmpty()

    def push(self, e):
        self.__list.addFirst(e)

    def pop(self):
        return self.__list.removeFirst()

    def peek(self, e):
        return self.__list.getFirst()

    def __str__(self):
        res = ('Stack:top ')
        res += str(self.__list)
        return res
예제 #6
0
class LinkedListSet(Set):
    def __init__(self):
        self.link = LinkedList()

    def add(self, e):
        if (self.link.contains(e) != True):
            self.link.addFirst(e)

    def remove(self, e):
        self.link.removeElement(e)

    def contains(self, e):
        return self.link.contains(e)

    def getSize(self):
        return self.link.getSize()

    def isEmpty(self):
        return self.link.isEmpty()
    def test_remove_by_index(self):

        lst = LinkedList()
        self.assertRaises(ValueError, lst.remove_by_index, 0)

        lst.add(5)
        lst.add(3)
        lst.add(11)
        lst.add(20)
        lst.add(30)

        a = lst.remove_by_index(0)
        assert(a == 5)
        assert(str(lst) == "3 11 20 30")

        a = lst.remove_by_index(-1)
        assert(a == 30)
        assert(str(lst) == "3 11 20")

        a = lst.remove_by_index(2)
        assert(a == 20)
        assert(str(lst) == "3 11")
        """
        :type head: ListNode
        :rtype: bool
        """
        is_palindrome_list = LinkedList(head)
        second_half_list = LinkedList(is_palindrome_list.find_mid_node())
        reversed_second_half_list = LinkedList(second_half_list.reverse_list())
        print(Solution().compare_two_list(reversed_second_half_list, is_palindrome_list))

    @staticmethod
    def compare_two_list(list1, list2):
        current1 = list1.head
        current2 = list2.head
        while current1 and current2:
            try:
                if current1.val == current2.val:
                    flag = True
                else:
                    flag = False
                current1 = current1.next
                current2 = current2.next
            except AttributeError:
                flag = False
                break
        return flag


if __name__ == '__main__':
    list1 = LinkedList.test_list()
    Solution().is_palindrome(list1.head)
예제 #9
0
 def __init__(self):
     self.__list = LinkedList()
예제 #10
0
 def __init__(self):
     self.link = LinkedList()
예제 #11
0
            i += 1
        if i == k:
            nex = end.next
            end.next = None
            pre.next = reverse(begin)
            begin.next = nex

            pre = begin
            begin = nex
            i = 1
        else:
            return head
    return head


link = LinkedList()
print("=============before=========")
link.add(1)
link.add(2)
link.add(3)
link.add(4)
link.add(5)
link.add(6)
link.add(7)
link.add(8)
link.add(9)
link.print_list()
print("=============after=========")
# 需要构造头节点
head_node = Node(0)
head_node.next = link.head
예제 #12
0
# from .LinkedList import LinkedList
from DataStructure.LinkedList import LinkedList

linked_list = LinkedList()

linked_list.push(2)
linked_list.push(5)
linked_list.push(3)

linked_list.append(6)

# Insert 7 at the beginning. So linked list becomes 7->6->None
linked_list.push(7)

# Insert 1 at the beginning. So linked list becomes 1->7->6->None
linked_list.push(1)

# Insert 4 at the end. So linked list becomes 1->7->6->4->None
linked_list.append(4)

# Insert 8, after 7. So linked list becomes 1 -> 7-> 8-> 6-> 4-> None
linked_list.insertafter(linked_list.head.next, 8)

linked_list.printlist()
예제 #13
0
# -*- coding:utf-8 -*-
from DataStructure.LinkedList import LinkedList, Node
import sys


def removeP(p):
    if p is None or p.next is None:
        return False
    p.data = p.next.data
    tmp = p.next
    p.next = tmp.next
    return True


link = LinkedList()
link.add(1)
link.add(2)
link.add(3)
link.add(4)
link.add(5)
link.add(6)
cur = link.head
while cur:
    value = str(cur.data) + "->"
    sys.stdout.write(value)
    cur = cur.next

print()
print("========remove node 3======")
p = link.head.next.next
removeP(p)
 def test_add_list(self):
     lsta = LinkedList()
     lsta.add_list(1, 2, 3, 4, 5)
     assert(str(lsta) == "1 2 3 4 5")
    def test_remove(self):

        lst = LinkedList()
        self.assertRaises(ValueError, lst.remove, 2)

        lst.add(5)
        lst.add(3)
        lst.add(11)
        lst.add(20)
        lst.add(30)

        a = lst.remove(3)
        assert(a == 3)
        assert(str(lst) == "5 11 20 30")

        a = lst.remove(20)
        assert(a == 20)
        assert(str(lst) == "5 11 30")

        a = lst.remove(5)
        assert(a == 5)
        assert(str(lst) == "11 30")

        self.assertRaises(ValueError, lst.remove, 40)
    def test_modify(self):
        lst = LinkedList()
        lst.add(5)
        lst.add(3)
        lst.add(11)
        lst.add(20)
        lst.add(30)

        lst.modify(50, 2)
        assert(lst.get(2) == 50)
        assert(str(lst) == "5 3 50 20 30")

        lst.modify(99, 0)
        assert(lst.get(0) == 99)
        assert(str(lst) == "99 3 50 20 30")

        lst.modify(70, -1)
        assert(lst.get(-1) == 70)
        assert(str(lst) == "99 3 50 20 70")

        lst.modify(60, 4)
        assert(lst.get(4) == 60)
        assert(str(lst) == "99 3 50 20 60")
    def test_add_index(self):
        lst = LinkedList()

        lst.add(1, 0)
        assert(lst.__repr__() == "1")
        assert(lst.get_size() == 1)

        lst.add(2, 0)
        assert(lst.__repr__() == "2 1")
        assert(lst.get_size() == 2)

        lst.add(3, 2)
        assert(lst.__repr__() == "2 1 3")
        assert(lst.get_size() == 3)

        lst.add(19, -1)
        assert(str(lst) == "2 1 3 19")

        lst.add(22, 2)
        assert(str(lst) == "2 1 22 3 19")

        self.assertRaises(ValueError, lst.add, 4, -2)
        self.assertRaises(ValueError, lst.add, 4, 6)
    def test_add_normal_and_size(self):
        lst = LinkedList()
        assert(lst.get_size() == 0)

        lst.add(1)
        assert(lst.__repr__() == "1")
        assert(lst.get_size() == 1)

        n1 = Node(2)
        lst.add(n1)
        assert(lst.__repr__() == "1 2")
        assert(lst.get_size() == 2)

        n2 = Node(5)
        lst.add(n2)
        assert(lst.__repr__() == "1 2 5")
        assert(lst.get_size() == 3)
    def test_clear(self):
        lst = LinkedList()
        lst.clear()
        assert(lst.get_size() == 0)
        assert(str(lst) == "")

        lst.add(5)
        lst.add(3)
        lst.add(11)
        lst.add(20)
        lst.add(30)

        lst.clear()
        assert(lst.get_size() == 0)
        assert(str(lst) == "")