当前位置: 首页 > news >正文

力扣Hot100每日一题[1,3]

160. 相交链表

题目列表: LeetCode 热题 HOT 100

解法一

使用哈希集合(C++中是unordered_set,Java中是HashSet)存储链表 h e a d A headA headA 节点,判断 h e a d B headB headB 中的节点是否在哈希集合中。

  • 时间复杂度: O ( m + n ) O(m+n) O(m+n),其中 m m m n n n 是分别是链表 h e a d A headA headA h e a d B headB headB 的长度。需要遍历两个链表各一次。
  • 空间复杂度: O ( m ) O(m) O(m),其中 m m m 是链表 h e a d A headA headA 的长度。需要使用哈希集合存储链表 h e a d A headA headA 中的全部节点。

C++代码

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {unordered_set<ListNode *>set;ListNode *t = headA;while(t != nullptr){set.insert(t);t = t->next;}t = headB;while(t != nullptr){if(set.count(t)){return t;}t = t->next;}return nullptr;}
};

Java代码

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {Set<ListNode> set = new HashSet<ListNode>();ListNode temp = headA;while(temp != null){set.add(temp);temp = temp.next;}temp = headB;while(temp != null){if(set.contains(temp)){return temp;}temp = temp.next;}return null;}
}

解法二

相交链表(双指针,清晰图解)添加链接描述

C++代码

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {ListNode *a = headA, *b = headB;while(a != b){a = (a != nullptr) ? a->next : headB;b = (b != nullptr) ? b->next : headA;}return a;}
};

Java代码

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode a = headA, b = headB;while(a != b){a = (a == null) ? headB : a.next;b = (b == null) ? headA : b.next;}return a;}
}

236. 二叉树的最近公共祖先

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if(root == p || root == q || root == nullptr) return root;TreeNode *left = lowestCommonAncestor(root->left, p, q);TreeNode *right = lowestCommonAncestor(root->right, p, q);if(left != nullptr && right != nullptr) return root;else if(left != nullptr) return left;else if(right != nullptr) return right;return nullptr; }
};
/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {//if(root == p || root == q || root == null) return root;TreeNode left = lowestCommonAncestor(root.left, p, q);TreeNode right = lowestCommonAncestor(root.right, p ,q);if(left == null) return right;if(right == null) return left;return root;}
}

234. 回文链表

206. 反转链表

迭代或递归实现
C++版本

//迭代
class Solution {
public:ListNode* reverseList(ListNode* head) {ListNode *pre = nullptr, *now = head;while(now != nullptr){ListNode *next = now->next;now->next = pre;pre = now;now = next;}return pre;}
};
// 递归
class Solution {
public:ListNode* reverseList(ListNode* head) {if(head == nullptr || head->next == nullptr) return head;ListNode *newhead = reverseList(head->next);head->next->next = head;head->next = nullptr;return newhead;}
};

Java版本

// 迭代
class Solution {public ListNode reverseList(ListNode head) {ListNode pre = null;while(head != null){ListNode temp = head.next;head.next = pre;pre = head;head = temp;}return pre;}
}
// 递归
class Solution {public ListNode reverseList(ListNode head) {if(head == null || head.next == null) return head;ListNode newHead = reverseList(head.next);head.next.next = head;head.next = null;return newHead;}
}

回归本题
C++版本

class Solution {
public:ListNode* reverseList(ListNode* head) {ListNode *pre = nullptr, *now = head;while(now != nullptr){ListNode *next = now->next;now->next = pre;pre = now;now = next;}return pre;}bool isPalindrome(ListNode* head) {ListNode *slow = head, *quick = head;while(quick->next != nullptr){slow = slow->next;quick = quick->next;if(quick->next != nullptr) quick = quick->next;}ListNode *rear = reverseList(slow);ListNode *a = head, *b = rear;while(a != slow){if(a->val != b->val) return false;a = a->next;b = b->next;}return true;}
};
class Solution {public ListNode reverseList(ListNode head) {ListNode pre = null;while(head != null){ListNode temp = head.next;head.next = pre;pre = head;head = temp;}return pre;}public boolean isPalindrome(ListNode head) {ListNode slow = head, quick = head;while(quick.next != null){slow = slow.next;quick = quick.next;if(quick.next != null) quick = quick.next;}ListNode rear = reverseList(slow), a = head, b = rear;while(a != slow){if(a.val != b.val) return false;a = a.next;b = b.next;}return true;}
}
http://www.xdnf.cn/news/972667.html

相关文章:

  • 【CF】Day80——Codeforces Round 872 (Div. 2) C⭐D (思维 + 模拟 | 树 + 思维 + 组合数学 + 分数取模)
  • 小天互连IM:信创体系下的安全、高效即时通讯新选择
  • 【小记】2024-2025生物计算类热点问题
  • 方案解读:智慧银行反欺诈大数据管控平台建设方案【附全文阅读】
  • 20、React常用API和Hook索引
  • Memory Repair (三)
  • Java单列模式总结及实现
  • asio之读写
  • 路径规划算法概论:从理论到实践
  • switch选择语句
  • ABB UNITROL 6000 X-power 3BH022294R0103 GFD233A103
  • Python 3.6/3.8版本切换脚本
  • 调用支付宝接口响应40004 SYSTEM_ERROR问题排查
  • Python模块全解析:从入门到精通
  • MySQL学习之---索引
  • Lighttpd 配置选项介绍
  • 谷歌趋势自动报告系统(Pipedream + Scrapeless + Discord)
  • 电脑一段时间没用就变成登陆的界面
  • 5G+边缘计算推动下的商品详情API低延迟高效率新方案
  • 【Linux Learning】SSH连线出现警告:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
  • 超火的开源项目(Github热点)
  • 交叉编译笔记
  • Docker部署Nginx-UI
  • 【CSS position 属性】static、relative、fixed、absolute 、sticky详细介绍,多层嵌套定位示例
  • 安装 PyCharm
  • Open3D 点云处理笔记
  • 城市照明深夜全亮太浪费?智能分时调光方案落地贵州某市
  • threadlocal的实现说明
  • python46
  • 端到端自动驾驶研究:通过强化学习与世界模型的协同作用向VLA范式演进