android 資料庫模糊查詢語句_mysql資料庫規範(開發角度)
阿新 • • 發佈:2021-01-09
技術標籤:leetcode#連結串列指標連結串列單鏈表演算法leetcode
迭代好寫多了,3個指標即可,最後當cur走到空時,pre就是新連結串列的頭
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
//先迭代,三個指標解決
//從頭走到尾,記錄當前指標,前驅指標和後繼指標
/*if(head == NULL || head->next == NULL) return head;
ListNode* pre,*cur,*hou;
pre = head;
cur = head->next;
hou = cur;
pre->next = NULL;
//當cur走到連結串列結束(null)時,pre就是新連結串列的頭部
while(cur != NULL){
hou = cur->next;
cur->next = pre;
pre = cur;
cur = hou;
}
return pre;*/
//遞迴
if(head == NULL || head->next == NULL) return head;
//在當前這一步反轉自己,讓自己的後繼結點指向自己
ListNode* next = head->next;
head->next = NULL;
return helpReverse(head,next);
}
ListNode* helpReverse(ListNode* cur,ListNode* node){
if(node == NULL ) return cur;
ListNode* next = node->next;
node->next = cur;
return helpReverse(node,next);
}
};