vault backup: 2022-09-20 14:26:40

This commit is contained in:
juan 2022-09-20 14:26:40 +08:00
parent c6b62f472d
commit 0b590d485d

View file

@ -102,7 +102,12 @@ The iteration method use two pointers:
- one that points to the new head, as an anchor - one that points to the new head, as an anchor
- The other one points to nodes we need to iterate over: - The other one points to nodes we need to iterate over:
The iterator **has** to be pointing to head, and use
`cur->next` instead of directly using cur, otherwise will
lead to strange loops or bugs.
The pointer moves by `cur->next = cur->next->next`, it
represents the head element.
### Solution ### Solution
@ -123,6 +128,7 @@ public:
// Move cur->next after pre. // Move cur->next after pre.
pre->next = cur->next; pre->next = cur->next;
// Fix pointers, because cur->next is unchanged when changing position. // Fix pointers, because cur->next is unchanged when changing position.
// This part is important, must use cur->next.
cur->next = cur->next->next; cur->next = cur->next->next;
pre->next->next = temp; pre->next->next = temp;
} }