Leetcode 链表
文章平均质量分 66
Leetcode中与链表有关的题目
努利!奋斗!
这个作者很懒,什么都没留下…
展开
-
2130. Maximum Twin Sum of a Linked List
leetcode原创 2022-07-31 02:58:26 · 210 阅读 · 0 评论 -
Leetcode 677. Map Sum Pairs
题目解法:使用前缀树或者字典树每个节点代表一个字母,利用map来储存相邻字符之间的关系这个题还需要后续深入理解struct TrieNode{ TrieNode* child[26] = {}; int sum = 0;};class MapSum {public: TrieNode trieRoot; unordered_map<string,int> map_; void insert(string key, int v原创 2022-04-15 13:17:04 · 334 阅读 · 0 评论 -
Leetcode 141:Linked List Cycle python
Leetcode 141:Linked List Cycle题目解法1解法2题目Given a linked list, determine if it has a cycle in it.To represent a cycle in the given linked list, we use an integer pos which represents the position (0-i...原创 2019-12-25 06:42:34 · 524 阅读 · 0 评论 -
Leetcode 1721. Swapping Nodes in a Linked List
题目解析这个题目有如下的几个问题需要解决:如何在遍历链表时找到两个需要被交换的节点位置从头开始的第k个节点很好找,从结尾开始的第k个节点比较难找。比较笨的办法是先遍历一边列表,拿到长度,根据长度确定后面那个的位置。比较聪明的办法在下面解法会讲到找到后如何交换交换的话就会需要定位到两个节点分别的前后节点,实际上只需要两个节点的前面节点,后面可以通过next得到如何处理特殊情况有三种特殊情况需要处理解法1:交换值首先这边讲下比较聪明的定位从结尾开始的第k个节点。第一种方法:先便利找到顺原创 2022-04-05 13:21:58 · 284 阅读 · 0 评论 -
Leetcode 92. Reverse Linked List II
题目解题思路剥离出需要反转的子链表将反转后的子链表接回去-两个麻烦的点:如何剥离出子链表如何保留剥离后两端的节点并且接回去解法1:最开始很丑陋的解法,用一个while loop,通过现在的位置,保存需要保存的节点以及把子链表剥离开,用Reverse Linked List辅助反转子列表,再将子列表接回去class Solution {public: ListNode* reverseList(ListNode* head) { ListNode *prev原创 2022-04-03 16:07:35 · 372 阅读 · 0 评论 -
Leetcode Merge Two Sorted Lists (python+cpp)
题目解法pythonclass Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: prehead = ListNode(-1) curr = prehead while l1 and l2: if l1.val<l2.val: curr.next = l1原创 2020-12-27 07:50:05 · 110 阅读 · 0 评论 -
Leetcode 2. Add Two Numbers (python+cpp)
题目解法1:链表转listclass Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: def to_int(l): s = '' while l: s += str(l.val) l = l.next return int(s[::-1]原创 2020-12-24 10:37:16 · 190 阅读 · 0 评论 -
Leetcode Remove Duplicates类型题目 (python)
26. Remove Duplicates from Sorted Array解法:two pointersclass Solution: def removeDuplicates(self, nums: List[int]) -> int: if not nums: return 0 i = 0 for j in range(1,len(nums)): if nums[i]原创 2020-12-15 04:50:39 · 453 阅读 · 0 评论 -
Leetcode 23. Merge k Sorted Lists (python+cpp)
Leetcode 23. Merge k Sorted Lists题目:解法1:heap解法2:divide and conquer题目:解法1:heap利用heap,保持heap size在k,这边在python里可以用priorityqueue也可以直接用heap。用priorityqueue的话python3不行,因为python3里面定义不太一样:python3 use from queue import PriorityQueue, instead of from Queue. (the原创 2020-09-07 05:49:10 · 629 阅读 · 0 评论 -
Leetcode 234. Palindrome Linked List (python+cpp)
Leetcode 234. Palindrome Linked List题目解法:题目解法:使用快慢指针找中点,然后反转后半部分列表进行对比python:class Solution: def isPalindrome(self, head: ListNode) -> bool: def reverse_list(head): prev = None curr = head while curr原创 2020-07-17 09:59:29 · 88 阅读 · 0 评论 -
Leetcode 160. Intersection of Two Linked Lists (python+cpp)
Leetcode 160. Intersection of Two Linked Lists题目解法:双指针法题目解法:双指针法双指针求链表交点是必须要掌握的方法。我们使用两个指针,分别指向两个链表的头节点,并以相同的速度前进,若到达链表结尾,则移动到另一条链表的头节点继续前进。按照这种前进方法,两个指针会在若干次前进后同时到达相交节点。python:class Solution: def getIntersectionNode(self, headA: ListNode, headB:原创 2020-07-17 09:51:07 · 215 阅读 · 0 评论 -
Leetcode 24. Swap Nodes in Pairs(python+cpp)
Leetcode 24. Swap Nodes in Pairs题目解法:iterative或recursion题目解法:iterative或recursion链表最基础的操作,必须要会的首先讲iterative的步骤:定义一个dummy node储存指向链表头部的指针,用来最后返回结果定义一个prev_node,参与交换过程,保存指向下一组交换的两个节点中交换后的第一个节点指针,这样才能track整条列表取出当前需要交换的两个节点,建议重新命名,这样就不会导致交换的时候很多next搞混原创 2020-07-16 09:58:44 · 175 阅读 · 0 评论 -
Leetcode 206. Reverse Linked List (python+cpp)
Leetcode 206. Reverse Linked List题目解法:recursive和iterative题目解法:recursive和iterativepython版recursive解法:class Solution: def reverseList(self, head: ListNode) -> ListNode: def reverse(head,prev): if not head: retu原创 2020-07-16 07:46:33 · 139 阅读 · 0 评论