A singly linked list with unknown length, how to quickly find the element in the middle?
WBOYOriginal
2016-07-25 09:08:131302browse
A singly linked list with unknown length, how to quickly find the element in the middle?
// Only ideas
// http://justjavac.iteye.com/blog/1497457
// Set two pointers, p1, p2, and start p1 and p2 at the head of the link.
// p1 takes two steps each time,
// p2 takes one step each time
// When p1 reaches the end of the linked list, the position of p2 is the middle element of the linked list
Copy code
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn