1.C++中指针的主要作用是指向node(让你知道你现在所指的位置和node 是哪个,让你关注于你现在要处理的node),str-next的作用是为了连接链表(防止链表断开)
2.p->next的变化,就是一个断舍离的过程——断了以前的,指向现在的。
3.指针的赋值如何理解(我指着它(node)呢,你也快指它啊!指上,对,指上,你就不会忘了,标记上了,我走了你正好帮我看着这个node,它很重要)
void reorderList(ListNode* head) {
if (!head || !head->next)
return;
// find the middle node: O(n)
ListNode *slow = head, *fast = head;
while (fast && fast->next && fast->next->next) {
slow = slow->next;
fast = fast->next->next;
}
// cut from the middle and reverse the second half: O(n)
ListNode *head2 = slow->next;
slow->next = NULL;
ListNode *q = head2->next;
head2->next = NULL;
while (q) {
ListNode *qNext = q->next;
q->next = head2;
head2 = q;//q指着一个Node,想让head2也知道它正在关注的东西,所以让它也指向它——相当于信息(地址)共享!
q = qNext;//qNext指着一个Node,想让q也知道它正在关注的东西,所以让它也指向它——相当于信息(地址)共享!
}
ListNode* p = head;
// merge two lists: O(n)
for (q = head2; p; ) {
auto t = p->next;
p->next = q;
p = p->next;
q = t;
}
/*
slow->next = reverse(slow->next);
ListNode* q = slow->next;
//reverse(slow->next);
while(p != slow && q ){
ListNode * qNext = q->next;
q->next = p->next;
p->next = q;
p = q->next;
q = qNext;
}
*/
}