做题记录
虚拟头节点
class Solution(object):
def removeElements(self, head, val):
dummy=ListNode(next=head)
cur=dummy
while cur.next!=None:
if cur.next.val!=val:
cur=cur.next
else:
cur.next=cur.next.next
return dummy.next
class Node(object):
def __init__(self,val):
self.val=val
self.next=None
class MyLinkedList(object):
def __init__(self):
self.dummy=Node(0)
self.count=0
def get(self, index):
if 0<=index<self.count:
cur=self.dummy
for _ in range(index+1):
cur=cur.next
return cur.val
else:return -1
def addAtHead(self, val):
self.addAtIndex(0,val)
def addAtTail(self, val):
self.addAtIndex(self.count,val)
def addAtIndex(self, index, val):
if index<0:index=0
elif index>self.count:return
self.count+=1
add=Node(val)
prev,cur=None,self.dummy
for _ in range(index+1):
prev,cur=cur,cur.next
else:prev.next,add.next=add,cur
def deleteAtIndex(self, index):
if 0<=index<self.count:
self.count-=1
prev,cur=None,self.dummy
for _ in range(index+1):
prev,cur=cur,cur.next
else:
prev.next,cur.next=cur.next,None
tmp存储cur.next
class Solution(object):
def reverseList(self, head):
prev,cur=None,head
while cur!=None:
tmp=cur.next
cur.next=prev
prev,cur=cur,tmp
return prev
class Solution(object):
def swapPairs(self, head):
dummy=ListNode(0,head)
cur=dummy
while cur.next!=None and cur.next.next!=None:
tmp=cur.next
tmp1=cur.next.next.next
cur.next=cur.next.next
cur.next.next=tmp
cur.next.next.next=tmp1
cur=cur.next.next
return dummy.next
fast指针先走n步,fast和slow再一起走,当fast走到末尾节点,slow节点在倒数第n个节点的前一个节点
class Solution(object):
def removeNthFromEnd(self, head, n):
dummy=ListNode(0,head)
l,r=dummy,dummy
while n!=0:
r=r.next
n-=1
while r.next!=None:
r=r.next
l=l.next
l.next=l.next.next
return dummy.next
判断链表是否有环:fast指针每次走两步,slow指针每次走一步,若能相遇则有环。
若有环,环入口为:一个指针指向头节点,一个指针指向相遇节点,每次一步,相遇处则为环入口
class Solution(object):
def detectCycle(self, head):
fast,slow=head,head
while fast!=None and fast.next!=None:
fast=fast.next.next
slow=slow.next
if fast==slow:
tmp1=head
tmp2=fast
while tmp1!=tmp2:
tmp1=tmp1.next
tmp2=tmp2.next
return tmp1
return None
class Solution(object):
def hasCycle(self, head):
dummy=ListNode(0,head)
slow,fast=dummy,dummy
while fast!=None and fast.next!=None:
slow=slow.next
fast=fast.next.next
if slow==fast:return True
return False
求AB之间链表长度差,让长链表指针指在短链表长度位置,同时移动找相同节点
class Solution(object):
def getIntersectionNode(self, headA, headB):
cura,curb=headA,headB
lena,lenb=0,0
#求长度
while cura!=None:
cura=cura.next
lena+=1
while curb!=None:
curb=curb.next
lenb+=1
cura,curb=headA,headB
##让cura指向最长链表
if lena<lenb:
lena,lenb=lenb,lena
cura,curb=curb,cura
step=lena-lenb
while step!=0:
cura=cura.next
step-=1
while cura!=None:
if cura==curb:return cura
else:
cura=cura.next
curb=curb.next
return None
数组模拟
class Solution(object):
def isPalindrome(self, head):
res=[]
cur=head
while cur!=None:
res.append(cur.val)
cur=cur.next
i,j=0,len(res)-1
while i<=j:
if res[i]==res[j]:
i+=1
j-=1
else:return False
return True
fast走两步,slow走一步,当fast走到末尾时slow停在中间,以slow为界划分前后两个链表,将slow后链表倒转,再与原链表比较
class Solution(object):
def isPalindrome(self, head):
fast,slow=head,head
while fast!=None and fast.next!=None:
slow=slow.next
fast=fast.next.next
node=None
while slow:
slow.next,slow,node=node,slow.next,slow
cur=head
while node:
if node.val==cur.val:
node=node.next
cur=cur.next
else:return False
return True
将头节点之后的节点全部存进队列
class Solution(object):
def reorderList(self, head):
from collections import deque
d=deque()
cur=head
while cur.next!=None:
d.append(cur.next)
cur=cur.next
cur=head
while len(d):
cur.next=d.pop()
cur=cur.next
if len(d):
cur.next=d.popleft()
cur=cur.next
cur.next=None
return head