Skip to content

Instantly share code, notes, and snippets.

@fangxlmr
Last active March 22, 2019 10:37
Show Gist options
  • Save fangxlmr/8ad620e6ed92b821a3911969e78d2f10 to your computer and use it in GitHub Desktop.
Save fangxlmr/8ad620e6ed92b821a3911969e78d2f10 to your computer and use it in GitHub Desktop.
Linked list issue solved in Python3
# 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