Posted on 2023-09-07 16:04
Uriel 阅读(37)
评论(0) 编辑 收藏 引用 所属分类:
数据结构 、
闲来无事重切Leet Code
翻转单链表[left, right]区间的节点
1 #92
2 #Runtime: 15 ms (Beats 63.77%)
3 #Memory: 13.7 MB (Beats 14.88%)
4
5 # Definition for singly-linked list.
6 # class ListNode(object):
7 # def __init__(self, val=0, next=None):
8 # self.val = val
9 # self.next = next
10 class Solution(object):
11 def reverseBetween(self, head, left, right):
12 """
13 :type head: ListNode
14 :type left: int
15 :type right: int
16 :rtype: ListNode
17 """
18 ex_head = ListNode()
19 ex_head.next = head
20 pre = ex_head
21 for _ in range(left - 1):
22 pre = pre.next
23 cur = pre.next
24 tp = None
25 for _ in range(right - left + 1):
26 nxt = cur.next
27 cur.next = tp
28 tp = cur
29 cur = nxt
30 pre.next.next = cur
31 pre.next = tp
32 return ex_head.next