[Linked list] returns the k-th node at the bottom of a single-chain table.

Source: Internet
Author: User

Algorithm 1]

Assume that a single-chain table has n nodes. If the last node is the reciprocal 1st nodes, the first n nodes are positive, and the corresponding relationship is (reciprocal => positive number ):

  • 1 => N
  • 2 => n-1
  • 3 => N-2
  • K => n-k + 1

Then, the number of the last K nodes is changed to the number of n-k + 1 nodes. First, we need to know how many nodes are there (traverse once) and find the forward n-k + 1 node (traverse again). The total time complexity is O (2n ).

Algorithm 2]

Set two pointers, Pa and Pb, to point at the same time to the header node. The PA pointer first moves the K-1 step, and then the two pointers move forward step by step at the same time. When the PA pointer reaches the end node, the PB Pointer Points to the nearest K node. This algorithm only uses a time series table. The time complexity is O (n ).

Algorithm 3]

The chain table is reversed. At this time, the End Node becomes the first node (traverse once), and then the K node can be searched from the first node (traverse again). The time complexity is O (2n ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.