contributed by < AxotZero
>
COND1 = head->next && head->val == head->next->val
COND2 = head->next && head->val == head->next->val
Haven't figured out whether there is another way to complete these two conditions.
Inspired from 案例探討: Leetcode 2095. Delete the Middle Node of a Linked List, I tried to use ptr2ptr to solve this problem and reduce branch of this program.
struct ListNode *deleteDuplicates(struct ListNode *head)
{
if (!head)
return NULL;
struct ListNode **ptr = &head, **cur = ptr;
while(*cur){
while((*cur)->next && (*ptr)->val == (*cur)->next->val){
cur = &(*cur)->next;
}
// there are duplicates
if(ptr != cur){
*ptr = (*cur)->next;
}
else{
ptr = &(*cur)->next;
}
cur = ptr;
}
return head;
}
Still figuring out the better way to simplify the code
list_for_each_entry_safe
lRUCacheFree
, It needs to delete all the container of each list_head
. Hence, we need a safe
pointer which represent the next node of current node that will be delete.list_for_each_entry
safe
pointer.list_last_entry
get
and put
value from cache, the program will move the target node to the first. Hence, the last element will be the least recently used element.TODO
for (int i = 0; i < n_size; i++) {
int len = 0;
int num;
node = find(nums[i], n_size, heads);
while (node) {
len++;
num = node->num;
list_del(&node->link);
int left = num, right = num;
while ((node = find(LLL, n_size, heads))) {
len++;
list_del(&node->link);
}
while ((node = find(RRR, n_size, heads))) {
len++;
list_del(&node->link);
}
length = len > length ? len : length;
--left
and RRR = ++right
++left
and RRR = --right
pre-decrement
/ pre-increment
operation to make program correct.TODO