反转链表
初始化三个指针
循环执行
temp = cur→next
cur→next = pre
pre = cur ; cur = temp
对于单链表,所有操作都是从头指针开始
// An highlighted block
struct ListNode* ReverseList(struct ListNode* pHead ) {// 三指针法struct ListNode* pre = pHead;struct ListNode* cur = pHead->next;struct ListNode* temp;if (pHead == NULL) return pHead;// 翻转while(cur != NULL){temp = cur->next; // m保存q的后继cur->next = pre; // 翻转pre = cur; // p,q一起往后移动一位cur = temp;}// 处理最开始一位的pHeadpHead->next = NULL;pHead = pre; // 此时最后一位是preturn pHead;
}