# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
head = ListNode(0)
cur = head
carry = 0
while l1 or l2 or carry:
if l1:
carry += l1.val
l1 = l1.next
if l2:
carry += l2.val
l2 = l2.next
cur.next = ListNode(carry%10)
carry = int(carry/10)
cur = cur.next
return head.next
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
head = ListNode(0)
cur = head
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
if l1:
cur.next = l1
if l2:
cur.next = l2
return head.next
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
head = cur = ListNode(0)
lists = [(i.val, i) for i in lists if i]
heapq.heapify(lists)
while lists:
cur.next = heapq.heappop(lists)[1]
cur = cur.next
if cur.next:
heapq.heappush(lists, (cur.next.val, cur.next))
return head.next
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
pre = None
cur = head
while cur:
nextNode = cur.next
cur.next = pre
pre = cur
cur = nextNode
return pre
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if fast==slow: return True
return False
19. Remove Nth Node From End of List
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
dummy = ListNode(0)
dummy.next = head
slow = dummy
fast = dummy
for i in range(0, n+1):
fast = fast.next
while fast:
slow = slow.next
fast = fast.next
slow.next = slow.next.next
return dummy.next
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if head is None: return True
slow = fast = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
pre, cur = None, slow.next
while cur:
nextNode = cur.next
cur.next = pre
pre, cur = cur, nextNode
while pre:
if head.val!=pre.val:
return False
head = head.next
pre = pre.next
return True
160. Intersection of Two Linked Lists
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
if not headA or not headB: return None
# get the end of list A
endA = headA
while endA.next:
endA = endA.next
endA.next = headB
# get the intersection
slow = headA
fast = headA
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow==fast: break
if slow!=fast:
endA.next = None
return None
fast = headA
while fast:
if fast==slow:
endA.next = None
return fast
fast = fast.next
slow = slow.next