Linear table-linked list

Source: Internet
Author: User

I. Linked list

The list of topics is generally not difficult, mainly to examine the coding ability.

1. Add list of linked lists

Given 2 linked lists, representing 2 non-negative integers, respectively. Their numbers are reversed in the linked list, and each node stores only one number. Calculates the number of the two and returns the and the linked header pointers.

"Analysis" Leetcode the 2nd question.

The advantage of reverse storage is that you can start at the beginning of the pointer, calculate the current bit and carry bits by bit, and put them into the list of representations and lists in turn.

"Extension idea" the above-mentioned reverse list is a very ingenious structure, which can also be used to achieve large number operations. (I used to use arrays to do =,=, the benefits of arrays can be randomly accessed, do multiplication convenient positioning, but the list does multiplication ...) Well, you need to use more auxiliary pointers ... )

"My Code: 4ms"

2. Link List Rollover

Given a list of links, flip the list m to nth position. Requires a direct rollover instead of applying for a new space. For example, given 1,2,3,4,5,m=2, n=4, returns 1,4,3,2,5.

"Analysis" leetcode the 92nd question.

The general use of the head interpolation method, the back of the node inserted into the head node in turn before, attention is in turn. In this way, only one pointer is required to record the reference node, and one pointer iterates over the node to be flipped, a counter.

"My Code" 0ms

3. Link List to Weight

Given the sorted list, delete the repeating element, preserving only the first occurrence of the node.

"Analysis" Leetcode the 83rd question.

Very simple, because the list is already in order, directly delete the following same element.

"My Code" 1ms.

4. Chain List Division

Given a linked list and a value x, the linked list is divided into two parts, so that nodes less than x are in front, and nodes greater than or equal to X are behind. In both parts, the order of occurrence in the original list should be maintained.

"Analysis" Leetcode the 86th question.

A bit similar to the partition process in a fast-line. But notice the special structure of the list, you can directly apply for 2 pointers, the nodes less than x are inserted after the first pointer, a node greater than or equal to the X is inserted after the 2nd pointer, and finally the 2 linked list is ready. The time complexity is n and the space is 2.

The "idea extension" quick sort can also be stored with a single-linked list structure. Note, however, that not all sorts are appropriate for a single-linked list structure, such as heap sorting.

"My Code" 1ms.

5. Single-Stranded public node problem

Given 2 unidirectional linked lists, the first common node of the two linked lists is calculated. If there is no public node, return NULL.

"Analysis" Leetcode the 160th question.

Note that it is a one-way list, so all nodes after the common node are the same, and it is impossible to have an X-shaped structure. Therefore, you can traverse 2 linked lists to get the length m,n. Then the long list goes |m-n| step, then synchronizes, until it points to the same node.

"Idea extension" if there is a ring in the list, you need to calculate the common node in the way of a fast and slow pointer. That is, 2 pointers, each moving 1 steps or 2 steps respectively.

"My Code" 2ms.

Linear table-linked list

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.