Esempio n. 1
0
    def reverse(self, head: ListNode):
        pre = None
        while head:
            nxt = head.next
            head.next = pre
            pre = head
            head = nxt
        return pre
        # 还是错了!
        # return head


# head1 = init_ln([1, 2, 2, 1])
# check(Solution().isPalindrome, [head1], True)
head1 = init_ln([1, 2, 5, 2, 1])
check(Solution().isPalindrome, [head1], True)
head1 = init_ln([1, 2, 5, 6, 2, 1])
check(Solution().isPalindrome, [head1], False)

"""

O(1)的空间,不考虑链表复原,快慢指针在寻找中间节点的过程中直接反转链表前半部分,找到中间节点之后直接从中间向两边开始比较

"""

"""
其实,借助二叉树后序遍历的思路,不需要显式反转原始链表也可以倒序遍历链表,下面来具体聊聊。

链表兼具递归结构,树结构不过是链表的衍生。那么,链表其实也可以有前序遍历和后序遍历
"""
Esempio n. 2
0
            next = curr.next
            curr.next = prev
            prev = curr
            curr = next
        # 返回的是prev
        return prev


"""
递归

返回末尾,倒数第二个(当前)指向末尾,所以当前(倒数第二个)的下一个(末尾)的下一个指向当前,然后将当前置空,返回末尾。

"""


class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if not head or head.next is None:
            return head
        new_head = self.reverseList(head.next)
        head.next.next = head
        head.next = None
        return new_head
        # 返回的是prev


head = init_ln([1, 2, 3, 4, 5, 6, 7, 8, 9])

print_ln(Solution().reverseList(head))
Esempio n. 3
0
        """
        pre上一个节点,head当前节点、next下一个节点

        当当前节点仍然有下一个节点的时候。将当前节点的方向反转, 移动到下一个节点继续操作。

        """
        tmp = head

        # 当前节点的前一个节点
        # 保留反转后的下一个节点
        pre = nxt = None
        # 保留下一个节点
        while head and k + 1 > 0:
            # 当前节点反转到前一个节点
            nxt = head.next
            head.next = pre
            # 顺序下去
            pre = head
            head = nxt
            k -= 1
        # 记录之前的头,作为非反转的头
        tmp.next = nxt
        return pre


head1 = init_ln([1, 2, 3, 4, 5])
print_ln(Solution().reverseTopK2(head1, 2))
# print_ln(Solution().reverseBetween(head1, 2, 4))
# print_ln(Solution().reverseBetween(init_ln([5]), 1, 1))
# print_ln(Solution().reverseBetween(init_ln([3, 5]), 1, 2))
Esempio n. 4
0
class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        cur = head
        while cur and cur.next:
            next = cur.next
            while next and next.val == cur.val:
                next = next.next
            cur.next = next
            cur = next
        return head


# head = init_ln([1, 2, 3, 4, 4, 9])
# head = init_ln([1, 2, 3, 3, 4, 4, 5])
head = init_ln([1, 2, 3, 3, 4, 4])

print_ln(Solution().deleteDuplicates(head))
"""

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/solution/shan-chu-pai-xu-lian-biao-zhong-de-zhong-49v5/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
"""


class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if not head:
            return head
        while l1 and l2:
            if l1.val < l2.val:
                dump.next = l1
                l1 = l1.next
            else:
                dump.next = l2
                l2 = l2.next
            dump = dump.next
        if l1:
            dump.next = l1
        else:
            dump.next = l2
        return tmp.next


head1 = init_ln([1, 3, 5, 7, 9])
head2 = init_ln([1, 2, 3, 4, 5, 6, 7, 8, 9])

print_ln(Solution().mergeTwoLists(head1, head2))

"""

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/solution/he-bing-liang-ge-you-xu-lian-biao-by-leetcode-solu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
"""


# 迭代
class Solution:
            while cur.next and cur.next.val == cur.val:
                cur = cur.next
                dup = True
            if dup:
                pre.next = cur.next
            else:
                # 因为pre为空节点,没有对下一个节点进行关联
                # 省下这一步的话,会导致tmp找不到下一个节点
                pre.next = cur
                pre = cur
            cur = cur.next
        return tmp.next


# head = init_ln([1, 2, 3, 4, 4, 9])
head = init_ln([1, 2, 3, 3, 4, 4, 5])

print_ln(Solution().deleteDuplicates(head))
"""

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/solution/shan-chu-pai-xu-lian-biao-zhong-de-zhong-oayn/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
"""


class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if not head:
            return head