面试题22
注意两个特殊输入,pHead为空,k==0
还有要提前返回空的,链表长度不足k的情况
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead ListNode类
* @param k int整型
* @return ListNode类
*/
ListNode* FindKthToTail(ListNode* pHead, int k) {
// write code here
//快慢指针,两者距离是1到k的距离,second指向最后一个节点
ListNode *first=pHead, *second=pHead;
//second往后走k-1步
if(pHead==nullptr) return nullptr;
if(k==0) return nullptr;
for(int i=0; i<k-1; ++i){
if(second->next !=nullptr)
second=second->next;
else
return nullptr;
}
while(second->next!=nullptr){
first=first->next;
second=second->next;
}
return first;
}
};
面试题23
快慢指针法,这个代码很简洁,注意分析画图部分。
另外,unordered_set也能处理,太简单,不能用。
参考题解
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead) {
ListNode* fast=pHead, *slow = pHead;
while(true){
if( fast==nullptr || fast->next==nullptr) return nullptr;
fast=fast->next->next;
slow=slow->next;
if(fast==slow) break;
}
fast=pHead;
while(fast != slow){
fast=fast->next;
slow=slow->next;
}
return fast;
}
};
面试题24
这题要熟练写出来。链表一定要画图。
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead==nullptr || pHead->next==nullptr) return pHead;
ListNode* pre=nullptr, *cur = pHead;
while(cur->next !=nullptr){
ListNode* temp=cur->next;
cur->next=pre;
pre=cur;
cur=temp;
}
cur->next=pre;//最后一个节点还没处理
return cur;
}
};
面试题25
写的不好看+慢,要优化一下写链表的速度和准确率了。
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
ListNode* dummyHead=new ListNode(0);
ListNode* cur1=pHead1, *cur2=pHead2, *cur=dummyHead;
while(cur1 != nullptr && cur2 !=nullptr){
if(cur1->val > cur2->val){
cur->next=cur2;
cur2=cur2->next;
cur=cur->next;
}
else{
cur->next=cur1;
cur1=cur1->next;
cur=cur->next;
}
}
if(cur1==nullptr) cur->next=cur2;
if(cur2==nullptr) cur->next=cur1;
return dummyHead->next;
}
};
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
ListNode* dummyHead=new ListNode(0);
ListNode* cur=dummyHead;
while(pHead1 && pHead2){
if(pHead1->val > pHead2->val){
cur->next=pHead2;
pHead2=pHead2->next;
}
else{
cur->next=pHead1;
pHead1=pHead1->next;
}
cur=cur->next;
}
if(pHead1)
cur->next=pHead1;
if(pHead2)
cur->next=pHead2;
return dummyHead->next;
}
};
面试题26
题解链接
时间复杂度O(MN) ,pRoot1每个节点递归比较pRoot2的每个节点,所以是MN,空间O(M)
class Solution {
public:
bool helper(TreeNode* p1, TreeNode* p2){
//p1和p2为根节点的子树是否有相同的结构
if(p2==nullptr) return true;//先判断p2,如果非空,p1是空就是false
if(p1==nullptr) return false;
if(p1->val != p2->val) return false;
else return helper(p1->left, p2->left) && helper(p1->right, p2->right);
}
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
if(pRoot2==nullptr) return false;
if(pRoot1==nullptr) return false;
return helper(pRoot1, pRoot2) || HasSubtree(pRoot1->left, pRoot2) || HasSubtree(pRoot1->right, pRoot2);
}
};
面试题27
自己写的swap递归,
时间O(N) 空间O(N)
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
TreeNode* Mirror(TreeNode* pRoot) {
// write code here
if(!pRoot) return nullptr;
if(!pRoot->left && !pRoot->right) return pRoot;
swap(pRoot->left, pRoot->right);
pRoot->left=Mirror(pRoot->left);
pRoot->right=Mirror(pRoot->right);
return pRoot;
}
};
官方题解,时间差不多,上面其实多了一行。
class Solution {
public:
TreeNode* Mirror(TreeNode* pRoot) {
// write code here
if(!pRoot) return nullptr;
TreeNode* left=Mirror(pRoot->left);
TreeNode* right=Mirror(pRoot->right);
pRoot->left = right;
pRoot->right = left;
return pRoot;
}
};
面试题28
class Solution {
public:
bool helper(TreeNode* left, TreeNode* right){
if(!left && !right) return true;
if(!left || !right) return false;
if(left->val != right->val) return false;
return helper(left->left, right->right) && helper(left->right, right->left);
}
bool isSymmetrical(TreeNode* pRoot) {
//注意对称问题考虑整个两个参数的函数来处理
if(!pRoot) return true;//空
return helper(pRoot->left, pRoot->right);
}
};