reverse_nodes_in_k_group
reverse_nodes_in_k_group.py 源码
# k个一组翻转链表
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
# 暴力法,使用一个数组存储
def reverseKGroup_1(self, head: ListNode, k: int) -> ListNode:
arr = []
while head:
arr.append(head.val)
head = head.next
dummy = ListNode()
cur, step = dummy, 0
for i in range(len(arr)):
if i == step and step + k <= len(arr):
p, q = i, i + k - 1
while p < q:
arr[p], arr[q] = arr[q], arr[p]
p, q = p + 1, q - 1
step += k
cur.next = ListNode(arr[i])
cur = cur.next
return dummy.next
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦