Esempio n. 1
0
 def mergeTwoLists(self, l1: Node, l2: Node) -> Node:
     node = Node()
     head = node
     while l1 and l2:
         if l1.val < l2.val:
             node.next = l1
             l1 = l1.next
         else:
             node.next = l2
             l2 = l2.next
         node = node.next
     if l1 is None:
         node.next = l2
     else:
         node.next = l1
     return head.next
Esempio n. 2
0
from leetcode.linked.Node import Node


class Solution:
    def mergeTwoLists(self, l1: Node, l2: Node) -> Node:
        node = Node()
        head = node
        while l1 and l2:
            if l1.val < l2.val:
                node.next = l1
                l1 = l1.next
            else:
                node.next = l2
                l2 = l2.next
            node = node.next
        if l1 is None:
            node.next = l2
        else:
            node.next = l1
        return head.next


if __name__ == '__main__':
    node1 = Node.init_by_list([1, 2, 4])
    node2 = Node.init_by_list([1, 3, 4])
    s = Solution()
    print(s.mergeTwoLists(node1, node2))
Esempio n. 3
0
    def slow_fast_pointer(self, head: ListNode) -> bool:
        if head is None or head.next is None:
            return True
        slow, fast = head, head
        pre = None
        while fast and fast.next:
            cur = slow
            slow = slow.next
            fast = fast.next.next
            cur.next = pre
            pre = cur
        if fast is not None:
            slow = slow.next
        while pre and slow:
            if pre.val != slow.val:
                return False
            slow = slow.next
            pre = pre.next
        return True
        # print(pre)
        # print(slow)
        # print(fast)


if __name__ == '__main__':
    node = ListNode.init_by_list([1, 2, 3, 4, 5])
    s = Solution()
    # print(s.isPalindrome(node))
    print(s.slow_fast_pointer(node))
Esempio n. 4
0
from leetcode.linked.Node import Node


def reverse(node):
    pre = None
    while node is not None:
        current = node
        node = node.next
        current.next = pre
        pre = current
    return pre


node1 = Node(1)
node2 = Node(2)
node1.set_next(node2)
node3 = Node(3)
node2.set_next(node3)

print(node1)
r = reverse(node1)
print(r)
Esempio n. 5
0
from leetcode.linked.Node import Node


class Solution:
    def delete_node(self, node):
        node.val = node.next.val
        node.next = node.next.next


if __name__ == '__main__':
    node = Node.init_by_list([4, 5, 1, 9])
    print(node)
    head = node
    d_node = None
    while head is not None:
        if head.val == 5:
            d_node = head
            break
        head = head.next
    s = Solution()
    s.delete_node(d_node)
    print(node)
Esempio n. 6
0
from leetcode.linked.Node import Node


def remove(node):
    cur = node
    while cur and cur.next:
        if cur.val == cur.next.val:
            cur.next = cur.next.next
        else:
            cur = cur.next


linked = Node.init_by_list([1, 1, 2, 3, 3])
remove(linked)
print(linked)