One way would be to iterate to the end of the list to get its size N then subtract m from N. Then iterator (N - m) elements from the beginning. This can potentially yield 2n complexity if m was small.
Another method could be from each node iterate to the next m elements and see if that element is has a next element that is null. This can also yield 2N if m is small.
Consider iterating from the first node m elements and keeping a pointer to the first node and the one at that node + m elements. Increment both pointers at the same time. Thus, when the latter pointer reaches the last node, the first node is the mth node from the last node. This gives N - m complexity.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment