前11题 敲了一遍
# 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:
p1 = headA
p2 = headB
while p1 != p2:
p1 = p1.next if p1 else headB
p2 = p2.next if p2 else headA
return p1
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
pre = None
cur = head
while cur:
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
res = ListNode(None)
node = res
while l1 and l2:
if l1.val<l2.val: # 注:比较链表节点大小时用.val
node.next = l1
l1= l1.next
else:
node.next = l2
l2 = l2.next
node = node.next
if l1:
node.next = l1
if l2:
node.next = l2
return res.next
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
res = head # 有时候也可以改变head。
while head and head.next: # while head.next:会报错。AttributeError: 'NoneType' object has no attribute 'next'。先执行head,head存在才会执行head.next,因为head如果是none,那么head就没有next属性,就会报错。所以写成while head.next and head:也是不行的。
if head.val == head.next.val:
head.next = head.next.next
else:
head = head.next
return res
# 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
"""
p1 = ListNode(None) # 记住:p1 = ListNode(None), p1.next = head就是为了在链表长度为1时,p1.next.next有意义。是ListNode不是NodeList
p2 = ListNode(None)
p1.next = head
p2.next = head
res = p2
while n>1:
p1=p1.next
n -= 1
while p1.next and p1.next.next:
p1 = p1.next
p2 = p2.next
p2.next = p2.next.next
return res.next
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
res = ListNode(None)
res.next = head
node = res
while node.next and node.next.next: # 先执行node.next,如果node.next是none的话,node.next就不存在next属性。
a, b = node.next, node.next.next
node.next, a.next = b, b.next
b.next = a
node = node.next.next
return res.next
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
res = ListNode(None)
node = res
carry = 0
while l1 or l2 or carry:
a = l1.val if l1 else 0
b = l2.val if l2 else 0
cur = a + b + carry
carry = cur // 10
cur = cur % 10
node.next = ListNode(cur)
node = node.next
if l1:
l1 = l1.next
if l2:
l2 = l2.next
return res.next
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
list1, list2 = [], []
while l1: # 注:这里是l1
list1.append(l1.val)
l1 = l1.next
while l2:
list2.append(l2.val)
l2 = l2.next
res = None # res = ListNode(None)会报错,会输出[7,8,0,7,None]而不是[7,8,0,7]
carry = 0
while list1 or list2 or carry: # 这里要记住carry
a = list1.pop() if list1 else 0
b = list2.pop() if list2 else 0
cur = a + b + carry
carry = cur // 10
cur %= 10
node = ListNode(cur)
node.next = res
res = node
return res
# 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
"""
sum1 = 0
sum2 = 0
node = head
t = 1
while node:
sum1 = sum1 + node.val * t
t *= 10
sum2 = sum2 * 10 + node.val
node = node.next
return sum1 == sum2
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def splitListToParts(self, root, k):
"""
:type root: ListNode
:type k: int
:rtype: List[ListNode]
"""
node = root
l = 0
res = []
while node:
l += 1
node = node.next
avg, rem = divmod(l,k)
node = root # 这句话不能省啊!!!!!!
for i in range(k):
head = node
for j in range(1,avg + (i < rem)): # 因为node初始为第一个节点,所以循环从1开始。
if node:
node = node.next
if node:
tmp = node.next
node.next = None
node = tmp
res.append(head)
return res
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
# 双指针法。
def oddEvenList(self, head: ListNode) -> ListNode:
if not head: # 这个不能丢,会出错
return
point1, point2 = head, head.next
p1, p2 = point1, point2
while p2 and p2.next:
p1.next = p1.next.next # 不要写成p1 = p1.next.next!!!!!!!
p2.next = p2.next.next
p1 = p1.next
p2 = p2.next
p1.next = point2
return point1
11.排序链表
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def sortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
# 时间复杂度O(nlogn)——>二分法,从而联想到归并排序。
# 归并排序的空间复杂度为O(n),由新开辟数组O(n)和递归函数调用O(logn)组成。
# 但链表可以通过修改引用来更改节点顺序,无需像数组一样开辟额外空间,递归调用函数将带来O(logn)的空间复杂度,因此若希望达到O(1)空间复杂度,则不能使用递归。
if not head or not head.next: # 写成if not head: return 会报错
return head
slow, fast = head, head.next
while fast and fast.next: # 偶数个节点找到中心左边的节点
slow, fast = slow.next, fast.next.next
mid, slow.next = slow.next, None # 将链表切断
l, r = self.sortList(head), self.sortList(mid) # 递归切断。
res = ListNode(0)
node = res
while l and r: # 合并左右链表。
if l.val < r.val:
node.next, l = l, l.next
else:
node.next, r = r, r.next
node = node.next
if l:
node.next = l
if r:
node.next = r
return res.next
12.判断链表中是否有环(牛客面试热题)
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
# 思路:快慢指针,if not fast:return false.if fast == slow:return True。
# 时间复杂度O(n),空间复杂度O(1)
class Solution:
def hasCycle(self , head):
# write code here
if not head or not head.next:
return False
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if not fast:
return False
if fast.val == slow.val:
return True
13.链表中环的入口节点(牛客面试热题)
思路:由f=s+nb,f=2s得到s=nb,slow再走a到达入口可转为head走a到达入口。要用一个while True来使循环找到相等的点。
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self , head ):
# write code here
slow = head
fast = head
while True:
if not fast or not fast.next: #if not fast:错。涉及两next都要判断next是否存在。
return
slow = slow.next # slow = slow.next, fast = fast.next.next不能放在if的上面,会报错。先判断,再赋值。要放在上面,必须在最前面加if not head or not head.next: return
fast = fast.next.next
if fast == slow:
break
node = head
while node != slow:
node = node.next
slow = slow.next
return node
14.链表中的节点每k个一组翻转
class Solution:
def reverseKGroup(self , head , k ):
# write code here
# 思路:计算长度l, l//k份,k次循环。tmp,cur,tmp,pre后面三个next
pre = ListNode(None)
cur = head
pre.next = head
res = pre
node = head
l = 0
while node:
l = l+1
node = node.next
for i in range(l//k):
for j in range(k-1):
tmp = cur.next
cur.next = tmp.next
tmp.next = pre.next
pre.next = tmp
pre = cur
cur = cur.next
return res.next
项目的难点是什么?