北京网站设计公司招聘信息,关键词排名工具,php的网站,2019做哪个网站赚钱题目来源#xff1a; leetcode题目#xff0c;网址#xff1a;110. 平衡二叉树 - 力扣#xff08;LeetCode#xff09;
解题思路#xff1a; 哈希表。遍历链表若当前元素在哈希表中#xff0c;则将其删除#xff0c;否则将其加入哈希表。
解题代码#xff1a;
/*…题目来源 leetcode题目网址110. 平衡二叉树 - 力扣LeetCode
解题思路 哈希表。遍历链表若当前元素在哈希表中则将其删除否则将其加入哈希表。
解题代码
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* removeDuplicateNodes(ListNode* head) {if(headnullptr){return head;}unordered_setint set;set.insert(head-val);ListNode* reshead;while(head-next!nullptr){if(set.find(head-next-val)!0){head-nexthead-next-next;}else{set.insert(head-next-val);headhead-next;}}return res;}
}; 总结 不使用临时缓存区只想到暴力遍历一种 方法。 官方题解给出了哈希表和两重循环暴力遍历两种解法。