hot100 -- 7.链表系列
1.相交链表
问题:给你两个单链表的头节点 headA
和 headB
,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null
。
方法:分别遍历两个链表(前后交替)
# 分别遍历两个链表找交点
# 如果有交点,必然在某个位置相遇,因为交点及以后是重合的部分
# 如果无交点,也必然同时结束(都是遍历两个链表)
def getIntersectionNode(headA, headB):pA, pB = headA, headB# 遍历找交点while pA != pB:pA = pA.next if pA else headB # 遍历完A,遍历BpB = pB.next if pB else headA # 遍历完B,遍历Areturn pA # 交点/None
2.反转链表
问题:给你单链表的头节点 head
,请你反转链表,并返回反转后的链表。
方法:画图
一定要画图!!!
# 画图!!!
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = next# 创建链表(虚拟节点)
def CreateList(nums):dummy = ListNode()p = dummyfor num in nums:p.next = ListNode(num)p = p.nextreturn dummy.next # 返回头结点# # 创建链表(自实现)
# def CreateList(nums):
# head = ListNode(nums[0])
# p = head
# for i in range(1, len(nums)):
# p.next = ListNode(nums[i])
# p = p.next
# return head# 输出链表
def Print(p):while p:print(p.val)p = p.next# 反转链表(画图!!!)
def Reverse(head):pre, cur = None, headwhile cur:temp = cur.nextcur.next = prepre, cur = cur, tempreturn prehead = CreateList([1,2,3,4,5])
Print(head)
re_head = Reverse(head)
Print(re_head)
3.回文链表
方法1:原链表 == 反转链表
# 方法1:原链表 == 反转链表(注意要对原链表进行备份,链表反转之后,原链表也会跟着改变)
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = next# 创建链表
def CreateList(nums):dummy = ListNode()p = dummyfor num in nums:p.next = ListNode(num)p = p.nextreturn dummy.nextdef Print(p):while p:print(p.val)p = p.next# 链表备份
def CopyList(pA):dummy = ListNode()pB = dummywhile pA:pB.next = ListNode(pA.val)pB = pB.nextpA = pA.nextreturn dummy.next# 翻转
def Reverse(head):pre, cur = None, headwhile cur:temp = cur.nextcur.next = prepre, cur = cur, tempreturn pre# 判断回文
def IsPalind(pA, pB):while pA and pB:if pA.val != pB.val:return FalsepA, pB = pA.next, pB.nextif pA or pB:return Falsereturn Truehead = CreateList([1,2,2,1])
Print(head)
copy_head = CopyList(head)
re_head = Reverse(head)
Print(re_head)
print(IsPalind(copy_head, re_head))
方法2:转数组 + 双指针
# 方法2:转数组 + 双指针
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = next# 创建链表
def CreateList(nums):dummy = ListNode()p = dummyfor num in nums:p.next = ListNode(num)p = p.nextreturn dummy.nextdef Print(p):while p:print(p.val)p = p.next# 判断回文(链表转数组,双指针直接判断前后元素)
def IsPalind(p):# 转数组nums = []while p:nums.append(p.val)p = p.nextleft, right = 0, len(nums)-1# 判断数组回文while left < right:if nums[left] != nums[right]:return Falseleft += 1right -= 1return Truehead = CreateList([1,2,2,1])
Print(head)
print(IsPalind(head))
方法3:快慢指针 + 反转
# 方法3:快慢指针 + 反转
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = next# 创建链表
def CreateList(nums):dummy = ListNode()p = dummyfor num in nums:p.next = ListNode(num)p = p.nextreturn dummy.nextdef Print(p):while p:print(p.val)p = p.next# 翻转
def Reverse(head):pre, cur = None, headwhile cur:temp = cur.nextcur.next = prepre, cur = cur, tempreturn pre# 判断回文(快慢指针 + 反转)
def IsPalind(head):slow, fast = head, head # 快慢指针(定位)while fast and fast.next:slow, fast = slow.next, fast.next.next# 反转(后半部分)re_slow = Reverse(slow)# 比较前后部分while re_slow:if head.val != re_slow.val:return Falsehead, re_slow = head.next, re_slow.nextreturn Truehead = CreateList([1,2,2,1])
# Print(head)
print(IsPalind(head))