IEG热题-链表
class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode *aheadA, *bheadB; while(a!b){ a a ? a-next : headB; b b ? b-next : headA; } return a; } };206.反转链表class Solution { public: ListNode* reverseList(ListNode* head) { ListNode* prev nullptr; ListNode* curr head; while (curr) { ListNode* next curr-next; curr-next prev; prev curr; curr next; } return prev; } };class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* preHead new ListNode(-1); ListNode* prev preHead; while (l1 ! nullptr l2 ! nullptr) { if (l1-val l2-val) { prev-next l1; l1 l1-next; } else { prev-next l2; l2 l2-next; } prev prev-next; } // 合并后 l1 和 l2 最多只有一个还未被合并完我们直接将链表末尾指向未合并完的链表即可 prev-next l1 nullptr ? l2 : l1; return preHead-next; } };class Solution { public: ListNode* reverseKGroup(ListNode* head, int k) { // 统计节点个数 int n 0; for (ListNode* cur head; cur; cur cur-next) { n; } ListNode dummy(0, head); ListNode* p0 dummy; ListNode* pre nullptr; ListNode* cur head; // k 个一组处理 for (; n k; n - k) { for (int i 0; i k; i) { // 同 92 题 ListNode* nxt cur-next; cur-next pre; // 每次循环只修改一个 next方便大家理解 pre cur; cur nxt; } ListNode* nxt p0-next; p0-next-next cur; p0-next pre; p0 nxt; } return dummy.next; } };class Solution{ public: vectorint twoSum(vectorint nums, int target){ unordered_mapint,int mm; for(int i 0; inums.size();i){ int find target-nums[i]; if(mm.count(find)!0){ //如果哈希表有记录则说明找到案 vectorint v{i,mm[find]}; return v; }; //没有找到则继续循环并且将当前数记录到哈希表 mm[nums[i]] i; } //因为保证有答案 这里不会执行 为了编译顺利 随便返回一个vector return vectorint(); } };