将一个节点数为 size 链表 m 位置到 n 位置之间的区间反转,要求时间复杂度
,空间复杂度
。
例如:
给出的链表为
,
,
返回
.
例如:
给出的链表为
返回
数据范围: 链表长度
,
,链表中每个节点的值满足
要求:时间复杂度
,空间复杂度
进阶:时间复杂度
,空间复杂度
class Solution: def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode: firstNode = ListNode(None) lastNode = ListNode(None) r = head while 1: if r.next == None: r.next = lastNode lastNode = r break else: r = r.next firstNode.next = head begin = firstNode head = firstNode revnums = n - m while m > 1: begin = begin.next m -= 1 r = begin.next p = begin.next.next q = p.next while revnums > 0: p.next = begin.next begin.next = p r.next = q p = q q = q.next revnums -= 1 while 1: if r.next.val == None: r.next = None break else: r = r.next return head.next
class Solution: def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: # 创建虚拟头节点,简化边界条件处理 dummy = ListNode(None) dummy.next = head prev = dummy # prev 指向区间的前一个节点 # 找到区间的前一个节点 for _ in range(m - 1): prev = prev.next # 反转区间内的节点 curr = prev.next # curr 指向区间的第一个节点 next_node = curr.next # next_node 指向 curr 的下一个节点 # 使用节点状态判断控制循环 while next_node and m < n: curr.next = next_node.next next_node.next = prev.next prev.next = next_node next_node = curr.next m += 1 return dummy.nextAI优化了我的程序。
from os import preadv # 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 #添加一个不变的节点 X_Node=ListNode(0) X_Node.next=head node_m=X_Node #获取头部最后一个不变的节点 i=0 if n==m: return head while i<m-1: node_m=node_m.next i+=1 #获取尾部第一个不变的节点 i=0 node_n=head while i<n-1: node_n=node_n.next i+=1 node_n_n=node_n.next #翻转 for i in range(m,n): temp=node_m.next #临时存储变更的节点 node_m.next=temp.next #将下一个节点向前 node_n.next=temp #变动的节点放再最后一个变动节点的尾部 temp.next=node_n_n #连接不变的节点 node_n_n=temp #更新不会变动的尾部节点 return X_Node.next
# 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