代码拉取完成,页面将自动刷新
'''
Problem Statement
Given the head of a LinkedList and a number ‘k’, reverse every ‘k’ sized sub-list starting from the head.
If, in the end, you are left with a sub-list with less than ‘k’ elements, reverse it too.
'''
#mycode
from __future__ import print_function
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def print_list(self):
temp = self
while temp is not None:
print(temp.value, end=" ")
temp = temp.next
print()
def reverse_every_k_elements(head, k):
# TODO: Write your code here
former, latter, last_last_node = None, head, head
i=0
while i<k and latter is not None:
temp = latter
latter = latter.next
temp.next = former
former = temp
i += 1
head = temp
while latter is not None:
former, latter, begin = None, latter, latter
i=0
while i<k and latter is not None:
temp = latter
latter = latter.next
temp.next = former
former = temp
i += 1
last_last_node.next=temp
last_last_node=begin
return head
def main():
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
head.next.next.next.next.next = Node(6)
head.next.next.next.next.next.next = Node(7)
head.next.next.next.next.next.next.next = Node(8)
print("Nodes of original LinkedList are: ", end='')
head.print_list()
result = reverse_every_k_elements(head, 3)
print("Nodes of reversed LinkedList are: ", end='')
result.print_list()
main()
#answer
from __future__ import print_function
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def print_list(self):
temp = self
while temp is not None:
print(temp.value, end=" ")
temp = temp.next
print()
def reverse_every_k_elements(head, k):
if k <= 1 or head is None:
return head
current, previous = head, None
while True:
last_node_of_previous_part = previous
# after reversing the LinkedList 'current' will become the last node of the sub-list
last_node_of_sub_list = current
next = None # will be used to temporarily store the next node
i = 0
while current is not None and i < k: # reverse 'k' nodes
next = current.next
current.next = previous
previous = current
current = next
i += 1
# connect with the previous part
if last_node_of_previous_part is not None:
last_node_of_previous_part.next = previous
else:
head = previous
# connect with the next part
last_node_of_sub_list.next = current
if current is None:
break
previous = last_node_of_sub_list
return head
def main():
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
head.next.next.next.next.next = Node(6)
head.next.next.next.next.next.next = Node(7)
head.next.next.next.next.next.next.next = Node(8)
print("Nodes of original LinkedList are: ", end='')
head.print_list()
result = reverse_every_k_elements(head, 3)
print("Nodes of reversed LinkedList are: ", end='')
result.print_list()
main()
'''
Time complexity
The time complexity of our algorithm will be O(N) where ‘N’ is the total number of nodes in the LinkedList.
Space complexity
We only used constant space, therefore, the space complexity of our algorithm is O(1).
'''
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。