160、相交链表
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
p = headA
q = headB
while p!=q:
p = p.next if p else headB
q = q.next if q else headA
return p
169、多数元素
class Solution:
def majorityElement(self, nums: List[int]) -> int:
nums.sort()
return nums[int(len(nums)/2)]
206、反转链表
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
p, rev = head, None
while p:
rev, rev.next, p = p, rev, p.next
return rev