拖延了以后还是来做题了qwq
哈希没补!补哈希!先做两道题补上今天和昨天的。
Num 237 删除链表中的节点
题非常简单,但是要注意两句话的顺序,一旦更新完next以后再访问真正的下一个的数值就访问不到了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void deleteNode(ListNode* node) { node->val=node->next->val; node->next=node->next->next; } };
Num 235二叉搜索树的最近公共祖先
注意一下是二叉搜索树全都符合左小右大,还有就是,如果两个数分别在左右两边,最近公共祖先就是root
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 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(p->val>root->val && q->val>root->val) return lowestCommonAncestor(root->right,p,q); if(p->val<root->val &&q->val<root->val) return lowestCommonAncestor(root->left,p,q); return(root); } };