-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
add a new reverse-linked-list #13228
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
Changes from 2 commits
abe29f6
a3138a8
27f956c
241f6ea
3320f4c
e25d0e9
cf3f002
fb195d2
5650e7d
43530a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| """ | ||
| https://www.enjoyalgorithms.com/blog/reverse-linked-list | ||
| """ | ||
|
|
||
|
|
||
| class ListNode: | ||
| """Definition for singly-linked list.""" | ||
|
|
||
| def __init__(self, val=0, next=None): | ||
|
Check failure on line 9 in data_structures/linked_list/reverse_linked_list.py
|
||
|
Sangram03 marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter:
Sangram03 marked this conversation as resolved.
|
||
| self.val = val | ||
| self.next = next | ||
|
|
||
|
|
||
| def reverse_linked_list(head: ListNode) -> ListNode: | ||
| """ | ||
| Reverse a singly linked list. | ||
|
|
||
| Args: | ||
| head: The head node of the linked list. | ||
|
|
||
| Returns: | ||
| The new head node of the reversed linked list. | ||
|
|
||
| Examples: | ||
| >>> a = ListNode(1) | ||
| >>> b = ListNode(2) | ||
| >>> c = ListNode(3) | ||
| >>> a.next, b.next = b, c | ||
| >>> head = reverse_linked_list(a) | ||
| >>> [head.val, head.next.val, head.next.next.val] | ||
| [3, 2, 1] | ||
| """ | ||
| prev = None | ||
| current = head | ||
| while current: | ||
| nxt = current.next | ||
| current.next = prev | ||
| prev = current | ||
| current = nxt | ||
| return prev | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() | ||
|
|
||
| # Example execution | ||
| a = ListNode(1) | ||
| b = ListNode(2) | ||
| c = ListNode(3) | ||
| a.next, b.next = b, c | ||
|
|
||
| print("Original Linked List: 1 -> 2 -> 3") | ||
| new_head = reverse_linked_list(a) | ||
| print( | ||
| f"Reversed Linked List: {new_head.val} -> {new_head.next.val} -> {new_head.next.next.val}" | ||
|
Check failure on line 57 in data_structures/linked_list/reverse_linked_list.py
|
||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.