链表的反转

算法复杂度:O(n)

 


void
Transfrom(LeetCode head,LeetCode& lc) { // head是带空节点的头指针: Intial(lc); LeetCode Q,P,R; if (IsEmpty(head)) { cout << "空表无法反转"; return; } P = head->next; Q = P->next; // 只有一个节点的链表 if (Q == NULL) { lc = head; return; } R = Q->next; // 这一步很关键:把最后的节点设置成空: P->next = NULL; while (true) { Q->next = P; P = Q; Q = R; if (Q == NULL) { lc->next = P; break; } R = R->next;// 为空咋办 } return; }

 

posted @ 2022-04-10 18:43  郭楠代码  阅读(31)  评论(0)    收藏  举报