Regarding finding the middle element of linked list

后端 未结 5 1617
轮回少年
轮回少年 2021-02-04 20:19

I am following the below approach to calculate the middle element from the linked list , but I want is there any built in method or any other approach which can als

5条回答
  •  北荒
    北荒 (楼主)
    2021-02-04 20:49

    Options:

    • Have a double linked-list and go from the back and front at the same time until you get to a common point.
    • Store the size of the list and simply stop when you've reached this half this size (similar to what the standard API's LinkedList does).

    Other than that I don't think you can do better than your algorithm.

提交回复
热议问题