-
-
Notifications
You must be signed in to change notification settings - Fork 47k
[NEW ALGORITHM] Rotate linked list by K. #9278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cclauss
merged 15 commits into
TheAlgorithms:master
from
Muhammadummerr:add_new_algorithm
Oct 1, 2023
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6e610d4
Rotate linked list by k.
Muhammadummerr 6122ded
Rotate linked list by k.
Muhammadummerr 64e0196
updated variable name.
Muhammadummerr a8058ad
Update data_structures/linked_list/rotate_linked_list_by_k.py
Muhammadummerr edfd0ec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1b9e7da
Update data_structures/linked_list/rotate_linked_list_by_k.py
Muhammadummerr e04ed07
Update data_structures/linked_list/rotate_linked_list_by_k.py
cclauss 589cb8c
Make Node a dataclass
cclauss c0a1f8e
Update rotate_linked_list_by_k.py
cclauss 34260bf
Merge branch 'TheAlgorithms:master' into add_new_algorithm
Muhammadummerr b575263
Update rotate_linked_list_by_k.py
cclauss 0644ed0
Update and rename rotate_linked_list_by_k.py to rotate_to_the_right.py
cclauss 69e4ddb
Update rotate_to_the_right.py
cclauss 69aa9fe
Update rotate_to_the_right.py
cclauss 5439cea
Update rotate_to_the_right.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Node: | ||
data: int | ||
next_node: Node | None = None | ||
|
||
|
||
def print_linked_list(head: Node | None) -> None: | ||
""" | ||
Print the entire linked list iteratively. | ||
|
||
This function prints the elements of a linked list separated by '->'. | ||
|
||
Parameters: | ||
head (Node | None): The head of the linked list to be printed, | ||
or None if the linked list is empty. | ||
|
||
>>> head = insert_node(None, 0) | ||
>>> head = insert_node(head, 2) | ||
>>> head = insert_node(head, 1) | ||
>>> print_linked_list(head) | ||
0->2->1 | ||
>>> head = insert_node(head, 4) | ||
>>> head = insert_node(head, 5) | ||
>>> print_linked_list(head) | ||
0->2->1->4->5 | ||
""" | ||
if head is None: | ||
return | ||
while head.next_node is not None: | ||
print(head.data, end="->") | ||
head = head.next_node | ||
print(head.data) | ||
|
||
|
||
def insert_node(head: Node | None, data: int) -> Node: | ||
""" | ||
Insert a new node at the end of a linked list and return the new head. | ||
|
||
Parameters: | ||
head (Node | None): The head of the linked list. | ||
data (int): The data to be inserted into the new node. | ||
|
||
Returns: | ||
Node: The new head of the linked list. | ||
|
||
>>> head = insert_node(None, 10) | ||
>>> head = insert_node(head, 9) | ||
>>> head = insert_node(head, 8) | ||
>>> print_linked_list(head) | ||
10->9->8 | ||
""" | ||
new_node = Node(data) | ||
# If the linked list is empty, the new_node becomes the head | ||
if head is None: | ||
return new_node | ||
|
||
temp_node = head | ||
while temp_node.next_node: | ||
temp_node = temp_node.next_node | ||
|
||
temp_node.next_node = new_node # type: ignore | ||
return head | ||
|
||
|
||
def rotate_to_the_right(head: Node, places: int) -> Node: | ||
""" | ||
Rotate a linked list to the right by places times. | ||
|
||
Parameters: | ||
head: The head of the linked list. | ||
places: The number of places to rotate. | ||
|
||
Returns: | ||
Node: The head of the rotated linked list. | ||
|
||
>>> rotate_to_the_right(None, places=1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: The linked list is empty. | ||
>>> head = insert_node(None, 1) | ||
>>> rotate_to_the_right(head, places=1) == head | ||
True | ||
>>> head = insert_node(None, 1) | ||
>>> head = insert_node(head, 2) | ||
>>> head = insert_node(head, 3) | ||
>>> head = insert_node(head, 4) | ||
>>> head = insert_node(head, 5) | ||
>>> new_head = rotate_to_the_right(head, places=2) | ||
>>> print_linked_list(new_head) | ||
4->5->1->2->3 | ||
""" | ||
# Check if the list is empty or has only one element | ||
if not head: | ||
raise ValueError("The linked list is empty.") | ||
|
||
if head.next_node is None: | ||
return head | ||
|
||
# Calculate the length of the linked list | ||
length = 1 | ||
temp_node = head | ||
while temp_node.next_node is not None: | ||
length += 1 | ||
temp_node = temp_node.next_node | ||
|
||
# Adjust the value of places to avoid places longer than the list. | ||
places %= length | ||
|
||
if places == 0: | ||
return head # As no rotation is needed. | ||
|
||
# Find the new head position after rotation. | ||
new_head_index = length - places | ||
|
||
# Traverse to the new head position | ||
temp_node = head | ||
for _ in range(new_head_index - 1): | ||
assert temp_node.next_node | ||
temp_node = temp_node.next_node | ||
|
||
# Update pointers to perform rotation | ||
assert temp_node.next_node | ||
new_head = temp_node.next_node | ||
temp_node.next_node = None | ||
temp_node = new_head | ||
while temp_node.next_node: | ||
temp_node = temp_node.next_node | ||
temp_node.next_node = head | ||
|
||
assert new_head | ||
return new_head | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
head = insert_node(None, 5) | ||
head = insert_node(head, 1) | ||
head = insert_node(head, 2) | ||
head = insert_node(head, 4) | ||
head = insert_node(head, 3) | ||
|
||
print("Original list: ", end="") | ||
print_linked_list(head) | ||
|
||
places = 3 | ||
new_head = rotate_to_the_right(head, places) | ||
|
||
print(f"After {places} iterations: ", end="") | ||
print_linked_list(new_head) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.