Last active
March 22, 2019 10:37
-
-
Save fangxlmr/8ad620e6ed92b821a3911969e78d2f10 to your computer and use it in GitHub Desktop.
Linked list issue solved in Python3
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
# Definition for singly-linked list. | |
# class ListNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.next = None | |
class LinkedList(object): | |
def get_middle(self, head: ListNode) -> ListNode: | |
fast = slow = head | |
while fast and fast.next: | |
slow = slow.next | |
fast = fast.next.next | |
return slow | |
def reverse(self, head: ListNode) -> ListNode: | |
prev, cur = None, head | |
while cur: | |
prev, cur.next, cur = cur, prev, cur.next | |
return prev | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment