徐州网站建设与推广,万网可以做网站吗,微管家平台,wordpress分类信息导航目录 206反转链表【链表结构基础】21合并两个有序链表【递归】我的答案【错误】自己修改【超出时间限制】在官方那里学到的【然后自己复写,错误】对照官方【自己修改】 160相交链表【未理解题目目的】在b站up那里学到的【然后自己复写,错误】【超出时间限制】对照官方【自己修改… 目录 206反转链表【链表结构基础】21合并两个有序链表【递归】我的答案【错误】自己修改【超出时间限制】在官方那里学到的【然后自己复写,错误】对照官方【自己修改】 160相交链表【未理解题目目的】在b站up那里学到的【然后自己复写,错误】【超出时间限制】对照官方【自己修改】 19删除链表的倒数第 N 个结点看完官方解题思路【自己复写错误】报错信息第一遍修改第二遍修改 148排序链表我的答案【冒泡排序最小的负数输出时变为0】官方答案【归并排序】 206反转链表【链表结构基础】
设定一个前结点和后结点。
class Solution(object):def reverseList(self, head)::type head: ListNode:rtype: ListNodepreNonecurheadwhile cur!None:tempcur.nextcur.nextpreprecurcurtempreturn pre21合并两个有序链表【递归】
我的答案【错误】
class Solution(object):def mergeTwoLists(self, list1, list2)::type list1: Optional[ListNode]:type list2: Optional[ListNode]:rtype: Optional[ListNode]resolListNode()while list1!None and list2!None:if list1.vallist2.val:resol.nextListNode(list1.val)else:resol.nextListNode(list2.val)list1list1.nextlist2list2.nextwhile list1!None:resol.nextListNode(list1.val)while list2!None:resol.nextListNode(list2.val)return resol.next自己修改【超出时间限制】
class Solution(object):def mergeTwoLists(self, list1, list2)::type list1: Optional[ListNode]:type list2: Optional[ListNode]:rtype: Optional[ListNode]resolListNode(0)tempresolwhile list1 and list2:if list1.vallist2.val:resol.nextlist1list1list1.next################else:resol.nextlist2list2list2.next################resolresol.next######################其实是不断增加结点的过程所以记得更新while list1:resol.nextlist1while list2:resol.nextlist2return temp.next在官方那里学到的【然后自己复写,错误】
class Solution(object):def mergeTwoLists(self, list1, list2)::type list1: Optional[ListNode]:type list2: Optional[ListNode]:rtype: Optional[ListNode]if list1 is None:return list2elif list2 is None:return list1else:if list1.vallist2.val:list1.nextmergeTwoLists(self,list1.next,list2)###如果只改mergeTwoLists(self,list1.next,list2)也是可以编译通过的运行成功else这里的逻辑也可以修正一下return list1else:list2.nextmergeTwoLists(self,list2.next,list1)return list2对照官方【自己修改】
class Solution(object):def mergeTwoLists(self, list1, list2)::type list1: Optional[ListNode]:type list2: Optional[ListNode]:rtype: Optional[ListNode]if list1 is None:return list2elif list2 is None:return list1elif list1.vallist2.val:list1.nextself.mergeTwoLists(list1.next,list2)########函数的调用return list1else:list2.nextself.mergeTwoLists(list2.next,list1)########函数的调用return list2160相交链表【未理解题目目的】
我看输入部分都已经给了要输出的结果了就不明白这题目的是要做什么。
在b站up那里学到的【然后自己复写,错误】
class Solution(object):def getIntersectionNode(self, headA, headB)::type head1, head1: ListNode:rtype: ListNodepheadA,qheadBwhile(p!q):p ? p.next : headBq ? q.next : headAprint(p.val)【超出时间限制】
class Solution(object):def getIntersectionNode(self, headA, headB)::type head1, head1: ListNode:rtype: ListNodepheadAqheadBwhile(p!q):if p.next !None:pp.nextelse:pheadBif q.next!None:qq.nextelse:qheadAreturn p对照官方【自己修改】
class Solution(object):def getIntersectionNode(self, headA, headB)::type head1, head1: ListNode:rtype: ListNodepheadA ####p,qheadA,headB这样可以qheadB ####pheadA,qheadB会报错while(p!q):p p.next if p else headBq q.next if q else headAreturn p19删除链表的倒数第 N 个结点
看完官方解题思路【自己复写错误】
class Solution(object):def removeNthFromEnd(self, head, n)::type head: ListNode:type n: int:rtype: ListNodepheadfor i in range(n1):pp.nextqheadwhile p!None:qq.nextpp.next###########【这里不能少啊】q.nextq.next.nextreturn head报错信息 查阅资料后发现【self.head是指向第一个元素的此时为None,所以没有next属性自然就会报错。因此第一个结点需要特殊处理我们一般通过增加头结点的方式来避免这种特殊处理。】
第一遍修改
发现当链表有n个元素时无法删除倒数第n个元素。【没有考虑到第一个结点的特殊性。】
class Solution(object):def removeNthFromEnd(self, head, n)::type head: ListNode:type n: int:rtype: ListNodepListNode(0, head)#################for i in range(n1):pp.nextqListNode(0, head)#################while p!None:qq.nextpp.nextq.nextq.next.next#########无法删除倒数第n个元素return head第二遍修改
class Solution(object):def removeNthFromEnd(self, head, n)::type head: ListNode:type n: int:rtype: ListNodedummyListNode(0, head)pdummyfor i in range(n1):pp.nextqdummywhile p!None:qq.nextpp.nextq.nextq.next.nextreturn dummy.next 148排序链表
我的答案【冒泡排序最小的负数输出时变为0】
class Solution(object):def sortList(self, head)::type head: ListNode:rtype: ListNodedummyListNode(0,head)qdummywhile q.next!None:pdummywhile p.next!None:if p.valp.next.val:tempp.valp.valp.next.valp.next.valtemppp.nextqq.nextreturn dummy.next官方答案【归并排序】
class Solution(object):def sortList(self, head):def sortFunc(head, tail):if not head:return headif head.next tail:head.next Nonereturn headslow fast headwhile fast ! tail:slow slow.nextfast fast.nextif fast ! tail:fast fast.nextmid slowreturn merge(sortFunc(head, mid), sortFunc(mid, tail))######合并两个升序链表但是长度差最多为1 def merge(head1, head2):dummyHead ListNode(0)temp, temp1, temp2 dummyHead, head1, head2while temp1 and temp2:if temp1.val temp2.val:temp.next temp1temp1 temp1.nextelse:temp.next temp2temp2 temp2.nexttemp temp.nextif temp1:temp.next temp1elif temp2:temp.next temp2return dummyHead.nextreturn sortFunc(head, None)