Leetcode 876. Middle of the Linked List

Mingyu Jeon·2020년 5월 3일
0
post-thumbnail

#Runtime: 20 ms, faster than 98.17% of Python3 online submissions for Middle of the Linked List.
#Memory Usage: 13.9 MB, less than 7.14% of Python3 online submissions for Middle of the Linked List.

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        cur = head
        count = 0
        while cur:
            count += 1
            cur = cur.next

        cur = head
        for i in range(count//2):
            cur = cur.next

        return cur

https://leetcode.com/problems/middle-of-the-linked-list/

0개의 댓글