Skip to content

Instantly share code, notes, and snippets.

@PttCodingMan
Last active June 13, 2020 05:20
Show Gist options
  • Save PttCodingMan/abe63f49e68d05c8bc987e11252c9f97 to your computer and use it in GitHub Desktop.
Save PttCodingMan/abe63f49e68d05c8bc987e11252c9f97 to your computer and use it in GitHub Desktop.
Reveres singly-linked list in Python 3
# 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