leetcode
_浮生_
More important things ahead, calm down and press forward with speed and urgency
展开
-
LeetCode - 链表求和
低位在前https://leetcode.cn/problems/sum-lists-lcci/ void handleRestBits(ListNode* cur, int extraOne) { while(extraOne) { int add = cur->val + extraOne; extraOne = add / 10; cur->val = add % 10原创 2022-05-29 14:44:51 · 154 阅读 · 0 评论 -
LeetCode - 二叉树每层的最大值
https://leetcode.cn/problems/hPov7L/ vector<int> largestValues(TreeNode* root) { if (root == nullptr) return {}; vector<int> res; queue<TreeNode*> q; q.push(root); while(!q.empty()原创 2022-05-24 20:09:10 · 189 阅读 · 0 评论 -
LeetCode - 翻转链表
https://leetcode.cn/problems/fan-zhuan-lian-biao-lcof/递归法 ListNode* reverseList(ListNode* head) { if (head == nullptr || head->next == nullptr) return head; auto ret = reverseList(head->next); head->next-&原创 2022-05-21 21:16:40 · 133 阅读 · 0 评论 -
Leetcode - 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as:a binary tree in which the left and right subtrees of every node differ in height by no more than 1.Example 1:Given the following tr原创 2020-08-20 22:51:14 · 91 阅读 · 0 评论 -
Leetcode - 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following [1,2,2,null,3,null,3] is not: 1 / \原创 2020-08-20 22:12:59 · 82 阅读 · 0 评论 -
Leetcode - 100. Same Tree
Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.Solution:/** * Definition for a binary tree node. * struct Tree原创 2020-08-17 22:44:00 · 111 阅读 · 0 评论 -
Leetcode - 143. Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You may not modify the values in the list’s nodes, only nodes itself may be changed.You must do this in-place without altering the nodes’ values.Example 1:Given 1->原创 2020-08-16 18:59:49 · 109 阅读 · 0 评论 -
Leetcode - 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.分析:假设这个链表如图所示,链表长度为L,相遇点是6节点,起始点到环开始点距离为a,环开始点到相遇点距离为x。设慢指针速度为1,快指针速度为2,相遇时间为t,则有:t = a + x ............(1)2t = L + x............(2)(2) - (1) 得:t = L - a原创 2020-08-16 18:02:37 · 109 阅读 · 0 评论 -
Leetcode - 141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.Solution:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bo原创 2020-08-16 16:26:58 · 102 阅读 · 0 评论 -
Leetcode - 138. Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.Example:Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]Output: [[7,null],[13,0],[11,4原创 2020-08-16 16:05:06 · 108 阅读 · 0 评论 -
Leetcode - 25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the e原创 2020-08-16 10:05:24 · 95 阅读 · 0 评论 -
Leetcode - 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.Your algorithm should use only constant space. You may not modify the values in the list’s nodes, only nodes itself may be changed.Example:Given 1->2->3->4, you should retur原创 2020-08-15 22:47:09 · 72 阅读 · 0 评论 -
Leetcode - 19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head.Example:Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the linked list becomes 1->2->3->5.Note:Given n w原创 2020-08-13 23:00:26 · 82 阅读 · 0 评论 -
Leetcode - 61. Rotate List
Given a linked list, rotate the list to the right by k places, where k is non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->NULLExplanation:rotate 1 steps to the right: 5->1->2->3-&g原创 2020-08-13 22:26:25 · 110 阅读 · 0 评论 -
Leetcode - 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.Return the linked list sorted as well.Example 1:Input: 1->2->3->3->4->4->5Output: 1->2->5Example 2:原创 2020-08-12 22:48:58 · 82 阅读 · 0 评论