LoginSignup
0
0

876. Middle of the Linked List

Posted at

876. 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: Optional[ListNode]) -> Optional[ListNode]:
        next_jud = head
        length = 1
        answer = head
        while next_jud is not None:
            next_jud = next_jud.next
            length += 1
        if(length % 2 == 0):
            low_ind = length / 2
        else:
            low_ind = (length // 2) + 1
        for i in range(1, int(low_ind)):
            answer = answer.next
        return answer
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0