Last active
June 13, 2020 05:20
-
-
Save PttCodingMan/abe63f49e68d05c8bc987e11252c9f97 to your computer and use it in GitHub Desktop.
Reveres singly-linked list in Python 3
This file contains 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
# Definition for singly-linked list. | |
# class ListNode: | |
# def __init__(self, val=0, next=None): | |
# self.val = val | |
# self.next = next | |
def get_reversed_linked_list(self, root): | |
current_node = root | |
next_node = root.next | |
pre_node = None | |
while next_node is not None: | |
current_node.next = pre_node | |
pre_node = current_node | |
current_node = next_node | |
next_node = current_node.next | |
current_node.next = pre_node | |
return current_node |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment