1 Star 0 Fork 0

shuangge/Grokking-the-Coding-Interview-Patterns-for-Coding-Questions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Reverse every K-element Sub-list (medium).py 3.43 KB
一键复制 编辑 原始数据 按行查看 历史
cl2333 提交于 5年前 . update
'''
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).
'''
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/shuangge666/Grokking-the-Coding-Interview-Patterns-for-Coding-Questions.git
git@gitee.com:shuangge666/Grokking-the-Coding-Interview-Patterns-for-Coding-Questions.git
shuangge666
Grokking-the-Coding-Interview-Patterns-for-Coding-Questions
Grokking-the-Coding-Interview-Patterns-for-Coding-Questions
master

搜索帮助