Linked lists and classic problems

Linked lists and classic problems
Linked lists and classic problems
Week 1-Linked lists and classic problems
(1) Basic knowledge of linked lists The structure of
linked lists The time complexity of accessing linked lists Several classic linked list implementation methods
(2) Typical application scenarios of linked lists
(3) The reversal of the access list of the classic interview question linked
list The node deletion of the linked list
(4) Easter egg exercises and related instructions

Basics (1) a list
structure linked list
node
data field pointer field
implementation includes an address, index (relative address), reference
chain structure
value of the pointer field is formed by a linear structure
of time complexity of the access list is
list is not suitable Fast positioning data, suitable for dynamic insertion and deletion application scenarios.
Find node O(Th), insert node O(1), delete node O(1).
Several classic linked list implementation methods.
Traditional method (node ​​+ pointer). Use array simulation.
Separate pointer field and data field.
Use array to store subscript for index
...

(2) Typical application scenarios of linked lists The
dynamic memory allocation in the operating system
LRU cache elimination algorithm
LRU = Least ReceThtly Used (least recently used) cache is a high-speed data structure.
There are speed differences between devices
. The system can be optimized by storing the more used data in the high-speed area and the less used content in the relatively low-speed area.

(3) Classic
Interview Questions Access to Linked List
LeetCode #141 Ring Linked List
Idea 1: Use a hash table (extra storage area) to store the nodes that have been traversed Idea 2: Double pointer approach
Use fast and slow pointers, fast pointers forward 2 at a time The node slow pointer
goes one node forward at a time . The fast pointer and the slow pointer in the ring
-shaped linked list will eventually meet in the ring. In the ring- free linked list, the fast pointer will be the first to visit the end of the linked list and terminate the detection process
LeetCode #142 Ring Linked List II
The distance traveled by the fast pointer is twice
that of the slow pointer. Consider the situation where the fast and slow pointers first meet (set the distance traveled by the slow pointer at this time as x). Specify a pointer p to be placed at the head of the linked list (p forwards 1 node each time) ) Take another distance for the length of x. The
slow pointer has reached the position of 2x. The pointer p has reached the position of x. The slow pointer and p have met.
Play back at the entrance of the ring. The slow pointer and p have already met. The
slow pointer and p overlap Walk a distance
LeetCode #202 Happy Number
Idea: Transform into a proof of convergence of the problem of judging whether there is a ring in the linked list. The
32-bit iTht represents a positive integer of approximately 2.1 billion ().
In this range, the largest number of the sum of squares of each digit is 1999999999 He is 730
according to the pigeon’s nest principle (pigeoThhole's priThciple, also translated as the drawer principle), there must be repetitions after 730 cycles

Inverted list
LeetCode # 206 inverted lists ideas 1: Iterative inversion
can be performed using the dummy head node to the first interpolation
ideas 2: reverse recursive (first node and a recursive process removed the remaining sub-list)
LeetCode # 92 trans
Tips for Turning Linked List II : Using dummy heads
is usually used when the first address of the linked list is likely to change.
LeetCode #25 K a group of flipped linked lists
Idea: First judge whether there are K elements and then check the K nodes Reverse and finally disassemble the first and last parts of
LeetCode #61 Rotating Linked List
Idea: After the whole linked list is connected end to end, go backwards by K, and then disassemble the ring.
LeetCode #24 The
idea of nodes in the pairwise exchange linked list is exactly the same as LeetCode #25. It is the simple case of K = 2.
Delete the node of the linked list
LeetCode #19 Delete the Nth node from the bottom of the linked list Idea: Find the previous node and adjust the pointer
LeetCode #83 Delete the duplicate node in the sorted linked list
LeetCode #82 Delete the duplicate node in the sorted linked list II

(4)
Easter egg exercises and related instructions Find the sum of all happy numbers in [0, 100000] (refer to LeetCode #202 for definition).

After finding the answer, replace the corresponding part in the link below with the value of the answer.
https://xue.kaikeba.com/api/meThtu/videos/answer.mp4

Guess you like

Origin blog.csdn.net/qq_45424679/article/details/114476462