Beispiel #1
0
    def test_is_palindrome(self):
        s = palindromeLinkedList.Solution()

        node1 = ListNode(1)
        node1.next = ListNode(2)
        self.assertEqual(s.isPalindrome(node1), False)

        node2 = ListNode(1)
        node2.next = ListNode(2)
        node2.next.next = ListNode(2)
        node2.next.next.next = ListNode(1)
        self.assertEqual(s.isPalindrome(node2), True)
Beispiel #2
0
    def test_linked_list_cycle(self):
        s = linkedListCycle.Solution()

        node = ListNode(3)
        node.next = ListNode(2)
        node.next.next = ListNode(0)
        end_node = ListNode(-4)
        end_node.next = node.next
        node.next.next.next = end_node
        self.assertEqual(s.hasCycle(node), True)

        node2 = ListNode(1)
        self.assertEqual(s.hasCycle(node2), False)
Beispiel #3
0
 def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
     newHead = ListNode(-1)
     newHead.next = head
     begin = newHead  # m-1 处
     # 遍历到逆转的起来
     i = 1
     while i < m:
         begin, i = begin.next, i + 1
     cur = begin.next
     start = begin.next # m 处
     pre = None
     while i <= n:
         tp, cur.next = cur.next, pre
         pre, cur, i = cur, tp, i + 1
     begin.next = pre
     if start:
         start.next = cur
     return newHead.next
 def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
     newHead = ListNode(-1)
     cur = newHead
     while l1 or l2:
         while l1:
             if l2 and l1.val > l2.val:
                 break
             cur.next = l1
             cur, l1 = cur.next, l1.next
         while l2:
             if l1 and l2.val > l1.val:
                 break
             cur.next = l2
             cur, l2 = cur.next, l2.next
     return newHead.next
 def mergeTwoLists2(self, l1: ListNode, l2: ListNode) -> ListNode:
     newHead = ListNode(-1)
     cur = newHead
     while l1 and l2:
         if l1.val < l2.val:
             cur.next, l1 = l1, l1.next
         else:
             cur.next, l2 = l2, l2.next
         cur = cur.next
     while l1:
         cur.next, l1 = l1, l1.next
         cur = cur.next
     while l2:
         cur.next, l2 = l2, l2.next
         cur = cur.next
     return newHead.next
                if l1 and l2.val > l1.val:
                    break
                cur.next = l2
                cur, l2 = cur.next, l2.next
        return newHead.next

    def mergeTwoLists2(self, l1: ListNode, l2: ListNode) -> ListNode:
        newHead = ListNode(-1)
        cur = newHead
        while l1 and l2:
            if l1.val < l2.val:
                cur.next, l1 = l1, l1.next
            else:
                cur.next, l2 = l2, l2.next
            cur = cur.next
        while l1:
            cur.next, l1 = l1, l1.next
            cur = cur.next
        while l2:
            cur.next, l2 = l2, l2.next
            cur = cur.next
        return newHead.next


l1 = ListNode(-9)
l1.next = ListNode(3)
l2 = ListNode(5)
l2.next = ListNode(7)
s = Solution()
s.mergeTwoLists2(l1, l2)
Beispiel #7
0
        while i < m:
            begin, i = begin.next, i + 1
        cur = begin.next
        start = begin.next # m 处
        pre = None
        while i <= n:
            tp, cur.next = cur.next, pre
            pre, cur, i = cur, tp, i + 1
        begin.next = pre
        if start:
            start.next = cur
        return newHead.next


s = Solution()
n5 = ListNode(5)
n4 = ListNode(4)
n4.next = n5
n3 = ListNode(3)
n3.next = n4
n2 = ListNode(2)
n2.next = n3
n1 = ListNode(1)
n1.next = n2
s = Solution()
r: list = s.reverseBetween(n1, 2, 4)
string = "["
if r:
    string += str(r.val)
    r = r.next
    while r:
        fast = head.next
        while fast and fast.next:
            slow, fast = slow.next, fast.next.next
        cur = slow.next
        slow.next = None

        # 反转后半部分
        pre = None
        while cur:
            tp, cur.next = cur.next, pre
            pre, cur = cur, tp

        # 重新合并两段
        left = head
        right = pre
        while left and right:
            tp_right = right.next
            right.next, left.next = left.next, right
            left, right = right.next, tp_right
        return head


l = ListNode(1)
l.next = ListNode(2)
l.next.next = ListNode(3)
l.next.next.next = ListNode(4)
l.next.next.next.next = ListNode(5)
s = Solution()
r = s.reorderList(l)
r.print()
Beispiel #9
0
# coding=utf-8
"""
@project : algorithmPython
@ide     : PyCharm
@file    : reverseList
@author  : illusion
@desc    : 206. 反转链表 https://leetcode-cn.com/problems/reverse-linked-list/
@create  : 2020-10-18 15:20:14
"""
from listNode.model import ListNode


class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if not head:
            return None
        pre = None
        while head:
            tp, head.next = head.next, pre
            pre, head = head, tp
        return pre


n3 = ListNode(3)
n2 = ListNode(2)
n2.next = n3
n1 = ListNode(1)
n1.next = n2
s = Solution()
s.reverseList(n1)