怒刷leetcode题目(3)226,83,142,86

226 Invert Binary Tree 36.2% Easy

Invert a binary tree.

     4
   /   \
  2     7
 / \   / \
1   3 6   9

to

     4
   /   \
  7     2
 / \   / \
9   6 3   1

对的,这就是前阵子homebrew大神面试没做出来的那道题

其实这道题并不难…也许只是大神不屑于做这样的题目罢了…

照样的,我们发现以下规律

所有的左子树和右子树交换,除非到了结点(树叶)

主需要交换两棵子树然后对子树递归就好了

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* invertTree(struct TreeNode* root) {
    if(root == NULL){
        return NULL;
    }
    struct TreeNode* temp = root->left;
    root->left=root->right;
    root->right = temp;
    invertTree(root->left);
    invertTree(root->right);
    return root;
}

83 Remove Duplicates from Sorted List 34.4% Easy

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

太简单了…都不知道说什么好了

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) {
    if(head == NULL){
        return false;
    }
    struct ListNode* pointer = head;
    while(pointer->next!=NULL){
        if(pointer->val==pointer->next->val){
            pointer->next = pointer->next->next;
        }else{
            pointer= pointer->next;
        }
    }
    return head;
}

142 Linked List Cycle II 31.4% Medium

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Follow up:
Can you solve it without using extra space?

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) {
    if(head==NULL){
        return false;
    }
    struct ListNode *slow = head;
    struct ListNode *fast = head;
    while(fast!=NULL && fast->next!=NULL){
        slow = slow->next;
        fast = fast->next->next;
        if(slow == fast){
            slow = head;
            while(slow!=fast){
                slow = slow->next;
                fast = fast->next;
            }
            return slow;
        }
    }
    return false;
}

这道题目跟141(上一篇博客中提及的)很类似,都是找出环

142实际是升级版,要找出环的入口

同样是使用快慢指针,大家可以在纸上画一下,写一下

如果有环,第一次相遇的时候,慢指针走了L+X的路程(L是起点到环入口的距离,X是入口到相遇时走过的距离,距离有可能比一圈的长度大)

快指针想当然的走了(L+X)*2的路程(其实不一定是2,只不过2比较好计算而已)

而且!!!快指针走过的距离是L+X+m*R(L、X同上,毕竟相遇点相同,m代表的是走过的圈数,R代表圈的长度)

也就是L+X=m*R

所以L=m*R-X

这时候,我们将慢指针回到起点,快指针的每一步的距离变成1

我们可以知道,慢指针和快指针相遇的时候

慢指针走了L,快指针走了m*R+m*R+X-X=2*m*R,也是在环的入口

所以他们再次相遇的节点就是环的入口

86 Partition List 27.4% Medium

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

遍历一次,将节点放进相应的链表,最后相连就好了,注意细节比较重要

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* partition(struct ListNode* head, int x) {
    if(head==NULL){
        return NULL;
    }
    if(head->next==NULL){
        return head;
    }
    struct ListNode* less_list = NULL;
    struct ListNode* less_list_head = NULL;
    struct ListNode* greater_list = NULL;
    struct ListNode* greater_list_head = NULL;
    struct ListNode* pointer = head;
    while(head!=NULL){
        struct ListNode* next = head->next;
        head->next = NULL;
        if(head->val<x){
            if(less_list_head==NULL){
                less_list_head=head;
                less_list=head;
            }else{
                less_list->next = head;
                less_list=less_list->next;
            }
        }else{
            if(greater_list_head==NULL){
                greater_list_head=head;
                greater_list=head;
            }else{
                greater_list->next = head;
                greater_list=greater_list->next;
            }
        }
        head=next;
    }
    if(less_list_head==NULL){
        return greater_list_head;
    }
    
    less_list->next=greater_list_head;
    return less_list_head;
}

点赞