21.Merge Two Sorted Lists
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def mergeTwoLists(self, l1, l2):
if l1==None:
return l2
if l2==None:
return l1
res=ListNode(0)#创建头节点
last=res#保存最近一个节点
while l1 and l2:#当前节点都不为空
if l1.val<l2.val:
last.next=l1
l1=l1.next
else:
last.next=l2
l2=l2.next
last=last.next#更新最近节点
last.next=l1 or l2#l1 or l2返回结果是不为空的一项
return res.next
22.Generate Parentheses
class Solution(object):
def generateParenthesis(self, n):
self.res=[]
self.parentIter('',n,n)
return self.res
def parentIter(self,tem,left,right):
if left==0 and right==0:#括号用完则加入答案
self.res.append(tem)
if right>0 and left<right:#剩余右括号多于左括号则继续加右括号)
self.parentIter(tem+")",left,right-1)
if left>0:#有左括号剩余则继续加左括号(
self.parentIter(tem+"(",left-1,right)
23.Merge k Sorted Lists
提交的时候总是提示错误AttributeError: ‘xxx’ object has no attribute ‘xxx’,找了半天发现是因为函数名和内置关键字冲突了,取一个有个性的名字还是很重要的。。。
设k个list,每个list的最大长度是n。时间复杂度O(nklogk),空间复杂度O(logk)
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
#k个有序链表排序
def mergeKLists(self, lists):
if len(lists)==0:
return []
if len(lists)==1:
return lists[0]
#排序
l1=self.mergeKLists(lists[:len(lists)//2])#调用自身
l2=self.mergeKLists(lists[len(lists)//2:])
res=self.mergeTwoLists(l1,l2)
return res
#两个有序链表排序
def mergeTwoLists(self,list1,list2):#将两个有序链表进行排序,返回头链接
if list1==None:
return list2
if list2==None:
return list1
#排序
tempRes=ListNode(0)
last=tempRes#保存上一个节点
while list1 and list2:
if list1.val<list2.val:
last.next=list1
list1=list1.next
else:
last.next=list2
list2=list2.next
last=last.next
last.next=list1 or list2#其中一个链表结束后,将另一个链表的剩余部分加到后面
return tempRes.next
24.Swap Nodes in Pairs
leetcode提供的链表不是有头链表,需要自己加一个头节点
# 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):
res=ListNode(0)#创建头节点
res.next=head
pre,cur=res,head#前一个节点
while cur and cur.next :
#删除cur节点
pre.next=cur.next
pre=pre.next
#在pre和next节点间插入cur
cur.next=pre.next
pre.next=cur
#向后移动指针
pre,cur=pre.next,cur.next
return res.next
25.Reverse Nodes in k-Group
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reverseKGroup(self, head, k):
#处理边界
if head==None or k==1 or head.next==None:
return head
res=ListNode(0)#创建头节点
lastend=res#指向已反转链表的最后一位
curstart=head#当前组的开头
temp=curstart
count=1#计数
while temp:#如果count=k,反转链表,否则指针向后移动,
if count==k:
nextstart=temp.next#下一组的开头
self.reverse(curstart,temp)
lastend.next=temp
lastend=curstart
curstart=nextstart
count=1
temp=curstart
continue
temp=temp.next
count+=1
lastend.next=curstart
return res.next
#将k节点链表反转
def reverse(self,start,end):
dummy=ListNode(0)#指向反转链表的最后一位
dummy.next = start
while dummy.next!=end:
temp=start.next
start.next=temp.next
temp.next=dummy.next
dummy.next=temp
26.Remove Duplicates from Sorted Array