Example #1
0
from leecode.common.common_class import ListNode
from leecode.common.official import stringToListNode


class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        dummy1 = head = ListNode(0)
        while l1 and l2:
            if l1.val <= l2.val:
                head.next = l1
                l1 = l1.next
            else:
                head.next = l2
                l2 = l2.next
            head = head.next
        if l1:
            head.next = l1
        if l2:
            head.next = l2
        return dummy1.next


arr1 = '[]'
lst1 = stringToListNode(arr1)

arr2 = '[]'
lst2 = stringToListNode(arr2)

s = Solution()
print(s.mergeTwoLists(lst1, lst2))
Example #2
0
from leecode.common.official import stringToListNode


class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:

        fast = head
        while n > 1:
            n -= 1
            fast = fast.next
        if not fast.next:
            return head.next
        fast = fast.next
        slow = head
        while fast.next:
            fast = fast.next
            slow = slow.next
        slow.next = slow.next.next
        return head


arr = '[1,2,3]'
n = 2
lst = stringToListNode(arr)

# p = head
# while p.next: p = p.next

s = Solution()
print(s.removeNthFromEnd(lst, n))
Example #3
0
tag: ^206 ^easy ^linkedlist
name: ^(Reverse Linked List)
'''


class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if head is None:
            return head
        stacks = []
        while head:
            stacks.append(head)
            head = head.next
        if stacks:
            top = stacks.pop()
        cur = top
        while stacks:
            cur.next = stacks.pop()
            cur = cur.next
        cur.next = None
        return top


h1 = stringToListNode('[2,6,4]')

t1 = datetime.now()
s = Solution()
print(s.reverseList(h1))
t2 = datetime.now()
print(t2 - t1)
Example #4
0
        l1 = headA
        l2 = headB
        while l1 is not l2:
            if l1.next or l2.next:
                l1 = headB if l1.next is None else l1.next
                l2 = headA if l2.next is None else l2.next
            else:
                return None
        else:
            return l1


# h1 = stringToListNode('[4,1]')
# h2 = stringToListNode('[5,0,1]')
# h3 = stringToListNode('[8,4,5]')
h1 = stringToListNode('[2,6,4]')
h2 = stringToListNode('[1,5]')
h3 = stringToListNode('[]')
top1 = h1
top2 = h2
while h1.next:
    h1 = h1.next
while h2.next:
    h2 = h2.next
if h3 is not None:
    h1.next = h3
    h2.next = h3

t1 = datetime.now()
s = Solution()
print(s.getIntersectionNode2(top1, top2))
Example #5
0
from datetime import datetime
from leecode.common.common_class import ListNode
from leecode.common.official import stringToListNode
'''
tag: ^1290 ^easy
name: ^(Convert Binary Number in a Linked List to Integer)
'''


class Solution:
    def getDecimalValue(self, head: ListNode) -> int:
        lst = []
        while True:
            lst.append(str(head.val))
            if head.next:
                head = head.next
            else:
                break
        return int(''.join(lst), 2)


arr1 = '[0,0]'
listnode = stringToListNode(arr1)

t1 = datetime.now()
s = Solution()
print(s.getDecimalValue(listnode))
t2 = datetime.now()
print(t2 - t1)
        if not head or not head.next:
            return False
        fast_pointer = head
        slow_pointer = head
        while slow_pointer and fast_pointer:
            slow_pointer = slow_pointer.next
            fast_pointer = fast_pointer.next
            if fast_pointer:
                fast_pointer = fast_pointer.next
            else:
                return False
            if slow_pointer == fast_pointer:
                return True
        return False


lst = '[3,2,0,-4]'
head = stringToListNode(lst)

p2 = head.next
p = head
while p.next:
    p = p.next
p.next = p2

# p = head
# while p.next: p = p.next

s = Solution()
print(s.hasCycle(head))
Example #7
0
                k %= (i - 1)
                if k == 0:
                    return head
                return find_k_node(head, head)

        return find_k_node(head, head)


# arr1 = '[1,2,3,4,5]'
# lst1 = stringToListNode(arr1)
# k = 2

# arr1 = '[0]'
# lst1 = stringToListNode(arr1)
# k = 4

# arr1 = '[0,1,2,3]'
# lst1 = stringToListNode(arr1)
# k = 5

# arr1 = '[0,1,2]'
# lst1 = stringToListNode(arr1)
# k = 4

arr1 = '[1,2,3,4,5]'
lst1 = stringToListNode(arr1)
k = 10

s = Solution()
print(s.rotateRight(lst1, k))
from leecode.common.official import stringToListNode

from leecode.common.common_class import TreeNode
from leecode.common.official import stringToTreeNode


class Solution:
    def deleteNode(self, node: ListNode):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        h = node
        while h:
            n = h.next
            h.val = n.val
            if not n.next:
                h.next = None
                break
            h = h.next


head = '[4,5,1,9]'
h = stringToListNode(head)
node = 5
t1 = datetime.now()
s1 = Solution()
print(s1.deleteNode(h))
t2 = datetime.now()
print(t2 - t1)