contributed by < williamlin0518
>
不用列出參考題解,專注於程式碼行為分析和你的改進。
left = &(*left)->next;
(*left)->next
: Accesses the next pointer of the current node.
&(*left)->next
: Takes the address of the next pointer of the current node, which is then assigned to left for the next iteration.
left = &(*left)->next;
Similar to list_tail
, it takes a pointer to a pointer to the first node of the list as its parameter.
CCCC
: p->next
DDDD
: list_tail(&left)
should be the last node of the left list
EEEE
: list_tail(&right)
should be the last node of the right list
Given the list: 4 -> 2 -> 6 -> 1 -> 3 -> 5, we choose the first element as the pivot for simplicity
After partitioning, our lists are:
After sorting each partition, we need to concatenate them in order:
Timsort is a hybrid sorting algorithm derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data.
If a run is smaller than this minimum size, Timsort will perform an insertion sort on this run and possibly adjacent elements to meets the minimum size.
struct list_head **tail = &head;
AAAA should be initialized to point to the head of the merged list. Since tail is used to track the end of the merged list as it's being built
tail = &(*tail)->next;
Both BBBB and CCCC are involved in updating the tail pointer after adding either a or b to the merged list.
DDDD :tail->next
EEEE :head->prev
less than or equal to 1 : direct build links
Let's illustrate the steps with a simple example. Consider a list: 5 -> 3 -> 4 -> 1 -> 2.
Initially, we identify runs in the list. Single elements are considered runs of length 1.
For our example, each element might be its own run since there's no clear ascending or descending order.
Based on run_size, we decide whether to merge adjacent runs. The decision is based on the lengths of the runs, aiming to merge smaller runs first to maintain stability and efficiency.
using the preorder array to determine root nodes and the inorder array to distinguish between left and right subtrees
"AAAA" : n->next = h->first;
assigns the new head's next pointer in the hlist_add_head function.
BBBB" : &heads[hash]
when iterates over the list starting from the head, pointing to the correct bucket in the hash table to search for the value.
list_entry
function that converts a list pointer to the enclosing structure.
list_entry: convert a pointer to struct hlist_node back into a pointer to the containing struct order_node.
&heads[hash]
adding nodes to a hash table, should be the head of the correct bucket
example to illustrate how buildTree works:
Preorder: [3, 9, 20, 15, 7]
Inorder: [9, 3, 15, 20, 7]
struct hlist_node
: includes not only a pointer to the next node (next) but also a pointer to the previous node's next pointer (pprev). This design allows for constant-time removal of the node from the list without traverseing the list to find the predecessor nodeEEEE: In the hlist_del function, this should be next->pprev. It is meant to update the pprev pointer of the next node in the list to point to the previous node's pprev pointer
FFFF: In the lRUCacheFree function, this should be link, which is the member of LRUNode that is used to link nodes in the doubly linked list.
&cache->link, the pointer to the link field within the LRUNode structure, indicating the node to be removed from the list.
HHH: In the lRUCacheGet function, this should be node, indicating the specific member within the LRUNode structure that is being used to navigate through the hash list.
IIII: In the lRUCacheGet function, calling list_move, it should be &cache->link, pointing to the link field within the LRUNode structure, indicating the node to be moved to the head of the list for LRU caching purposes.
within the hlist_for_each loop, this should also be node, as it refers to the member used to iterate through nodes in the hash list.
&c->link: indicating the specific node's position in the doubly linked list to be updated.
finding set bits (bits with value 1) within a large array of bits
If n is greater or equal to size, it means the requested set bit does not exist within the given size, so it returns size as an indication that the bit was not found.
FIND_NTH_BIT
macro is used for larger bitmaps. It iterates over each word in the bitmap:
a. If current word contains more than n set bits, finding the exact position of the Nth set bit within this word using fns
.