赞
踩
画图 -> 直观 + 形象 -> 便于理解
引入虚拟头结点
不要吝啬空间,大胆去定义变量
快慢双指针
new
⼀个结点储存最⾼位ListNode* AddTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* head = new ListNode(0); ListNode* cur1 = l1, *cur2 = l2; ListNode* tail = head; // 尾指针 int carry = 0; // 记录进位 & 临时数据 while(cur1 || cur2 || carry) { if(cur1) { carry += cur1->val; cur1 = cur1->next; } if(cur2) { carry += cur2->val; cur2 = cur2->next; } tail->next = new ListNode(carry % 10); tail = tail->next; carry /= 10; } ListNode* ret = head->next; delete head; return ret; }
ListNode* SwapPairs(ListNode* list) { // 边界处理 if(list == nullptr || list->next == nullptr) { return list; } ListNode *head = new ListNode(0); head->next = list; ListNode *prev = head, *cur = head->next, *next = cur->next, *nNext = next->next; while(cur && next) { // Swap prev->next = next; next->next = cur; cur->next = nNext; // Update prev = cur; cur = nNext; if(cur) { next = cur->next; } if(next) { nNext = next->next; } } ListNode *ret = head->next; delete head; return ret; }
void ReorderList(ListNode* head) { // 边界处理 if(!(head || head->next || head->next->next)) { return; } // 1.找到链表的中间结点 -> 快慢指针 ListNode *slow = head, *fast = head; while(fast && fast->next) // 偶 && 奇 { slow = slow->next; fast = fast->next->next; } // 2.逆序后半部分 -> 头插 ListNode *head2 = new ListNode(0); ListNode *cur = slow->next; slow->next = nullptr; // 断开两个链表 while(cur) { ListNode *next = cur->next; cur->next = head2->next; head2->next = cur; cur = next; } // 3.合并两个链表 -> 尾插 -> 双指针 ListNode *ret = new ListNode(0); ListNode *tail = ret; ListNode *cur1 = head, *cur2 = head2->next; while(cur1) { // 先连第一个链表 tail->next = cur1; tail = tail->next; cur1 = cur1->next; // 再连第二个链表 if(cur2) { tail->next = cur2; tail = tail->next; cur2 = cur2->next; } } delete head2; delete ret; }
// v1.0 Heap class Solution { struct Cmp { bool operator()(const ListNode* l1, const ListNode* l2) { return l1->val > l2->val; } }; public: ListNode* mergeKLists(vector<ListNode*>& lists) { // 创建一个小根堆 priority_queue<ListNode*, vector<ListNode*>, Cmp> heap; // 让所有头结点入堆 for(auto& list : lists) { if(list) { heap.push(list); } } // 合并K个有序链表 ListNode* ret = new ListNode(0); ListNode* tail = ret; while(!heap.empty()) { ListNode* tmp = heap.top(); heap.pop(); tail->next = tmp; tail = tail->next; if(tmp->next) { heap.push(tmp->next); } } tail = ret->next; delete ret; return tail; } }; // v2.0 递归 class Solution { public: ListNode* mergeKLists(vector<ListNode*>& lists) { return Merge(lists, 0, lists.size() - 1); } ListNode* Merge(vector<ListNode*>& lists, int left, int right) { // 边界情况处理 if(left > right) { return nullptr; } if(left == right) { return lists[left]; } // 中间点划分数组 int mid = left + (right - left) / 2; // [left, mid] [mid + 1, right] // 递归处理左右区间 ListNode* l1 = Merge(lists, left, mid); ListNode* l2 = Merge(lists, mid + 1, right); // 合并两个有序链表 return MergeTwoLists(l1, l2); } ListNode* MergeTwoLists(ListNode* l1, ListNode* l2) { // 边界情况处理 if(l1 == nullptr) { return l2; } if(l2 == nullptr) { return l1; } // 合并两有序链表 ListNode head(0); ListNode *cur1 = l1, *cur2 = l2, *tail = &head; while(cur1 && cur2) { if(cur1->val <= cur2->val) { tail->next = cur1; tail = tail->next; cur1 = cur1->next; } else { tail->next = cur2; tail = tail->next; cur2 = cur2->next; } } if(cur1) { tail->next = cur1; } if(cur2) { tail->next = cur2; } return head.next; } };
n /= 3
ListNode* ReverseKGroup(ListNode* head, int k) { // 遍历求n int n = 0; ListNode* cur = head; while(cur) { n++; cur = cur->next; } n /= k; // 重复n次逆序长度为k的链表 -> 头插 ListNode ret(0); ListNode *prev = &ret; cur = head; while(n--) { ListNode *back = cur; for(int i = 0; i < k; i++) { ListNode* next = cur->next; cur->next = prev->next; prev->next = cur; cur = next; } prev = back; // 更次每次头插的"头" } // 链接剩下的结点 prev->next = cur; return ret.next; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。