我写的输入与预期的输入和输出,我得到的…请解决输入为k=0..我附加的代码在这里
输入1 1. 4 5 7 8 3 36 -1 6. 正确输出1 36 3 8 7 5 4(我得到的)
输入2 1. 4 5 7 8 3 36 -1 0 我没有得到的预期输出 4577836
def kReverse(head, k):
current = head
next = None
prev = None
count = 0
while (current is not None and count < k):
next = current.next
current.next = prev
prev = current
current = next
count += 1
if next is not None:
head.next = kReverse(next, k)
return prev
```
此代码不会运行,因为您正在递归地反转k组中的每个节点。但问题是:“如果节点的数量不是k的倍数,那么被忽略的节点最终应该保持不变”
我在leetcode上运行您的代码:这是给定的链接列表:
您正在反转k组中的每个链接列表,并忽略链接列表的最后一部分是否小于k。这是你的结果:
这应该是结果:
以下是我的解决方案:
相关问题 更多 >
编程相关推荐