将一个节点数为 size 链表 m 位置到 n 位置之间的区间反转,要求时间复杂度 ,空间复杂度 。
例如:
给出的链表为 , ,
返回 .
例如:
给出的链表为 , ,
返回 .
数据范围: 链表长度 ,,链表中每个节点的值满足
要求:时间复杂度 ,空间复杂度
进阶:时间复杂度 ,空间复杂度
# class ListNode # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @param m int整型 # @param n int整型 # @return ListNode类 # class Solution: def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode: # write code here if head == None: return head a = ListNode(0) count, j = 1, -1 li = [] li1 = [] p1= head for i in range(1,n+1): #将单链表需反转数据赋给列表1 if i >= m: li1.append(p1.val) p1 = p1.next while head != None: #将反转与不反转的数据整合至一个列表中 p = head head = p.next p.next = a.next if count >= m and count <= n: # 将反转数据按排序后一一赋值 p.val = li1[j] j = j - 1 li.append(p.val) # 将所有数据一一放进列表中 count = count + 1 New_head = ListNode(li[0]) #头节点 cur = New_head #指针指向头节点 for i in li[1:]: # 将列表排好数据转换为单链表 node = ListNode(i) cur.next = node # 将列表数据赋给单链表下一个 cur = cur.next return New_head
class Solution: def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: if m == n: return head little,big = min(m, n),max(m, n) record_List = [] while head != None: record_List.append(head) head = head.next s,l = (big-little+1)//2, len(record_List) for i in range(s): record_List[little-1+i],record_List[big-1-i] = record_List[big-1-i],record_List[little-1+i] for i in range(l): if i < l-1: record_List[i].next = record_List[i+1] else: record_List[i].next = None return record_List[0]
class Solution: def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: # write code here newnode=ListNode(0) pre = newnode pre.next=head for i in range(1,m): pre=pre.next cur=pre.next for i in range(m,n): p=cur.next cur.next=p.next p.next=pre.next pre.next=p return newnode.next
class Solution: def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode: # write code here if head == None: return head if n <= m: return head new_p = None record_head = None record_tail = None count = 1 while head: if count < m: record_head = head head = head.next elif count > n: record_tail = head head = head.next # else count >= m and count <= n: else: tmp = head.next head.next = new_p new_p = head head = tmp count += 1 record_head.next = new_p q = record_head count = 0 while count < n-1: record_head = record_head.next count += 1 record_head.next = record_tail return q
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @param m int整型 # @param n int整型 # @return ListNode类 # class Solution: def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode: # write code here count = n-m cur = head pre = ListNode(0) pre.next = head M=m while m>1: pre=cur cur = cur.next m -=1 tmp1 = cur.next tail = cur tmp2 = tmp1.next if tmp1 else None while tmp1 and count: tmp2= tmp1.next tmp1.next = cur cur = tmp1 tmp1 = tmp2 count -=1 pre.next = cur tail.next = tmp1 return head if M>1 else pre.next
class Solution: def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode: # write code here temp = head res = [] length = 0 while temp is not None: length += 1 res.append(temp.val) temp = temp.next res1 = [] if n<length: res1 = res[:m-1]+res[m-1:n][::-1]+res[n:] else: res1 = res[:m-1]+res[m-1:n][::-1] temp = head i = 0 while temp is not None: temp.val = res1[i] temp = temp.next i += 1 return head
class Solution: def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode: # write code here newHead = ListNode(0) # 附加一个头结点,使得空链表和非空链表得到统一 newHead.next = head p = newHead # 反转部分的前一个指针,可以看做是反转部分的头结点 cur = p.next i = 1 while i < n: if i < m: p = cur cur = p.next else: # 永远想着把当前结点的后继变成反转部分的前驱,当前结点永远是反转部分的最后一个结点 pre = cur.next cur.next = pre.next pre.next = p.next p.next = pre i += 1 return newHead.next
class Solution: def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode: # write code here p = ListNode(0) p.next = head # 保证p.next存在 new_head = p cur = None i = 1 while i < n: if i < m: p = head head = head.next elif i >= m: cur = head.next head.next = cur.next # 保证第n个节点之后的节点不丢失 cur.next = p.next p.next = cur i += 1 return new_head.next