剑指offer专项突击版第9天

剑指 Offer II 027. 回文链表

O ( n ) O(n) O(n)时间复杂度和 O ( 1 ) O(1) O(1) 空间复杂度

class Solution {
public:
    ListNode* mid_node(ListNode* head) {
        ListNode *slow = head, *fast = head;
        while(fast->next != nullptr && fast->next->next != nullptr) {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }

    ListNode* reverse_list(ListNode *head){
        ListNode *pre = nullptr, *cur = head, *tmp;
        while(cur != nullptr) {
            tmp = cur;
            cur = cur->next;
            tmp->next = pre;
            pre = tmp;
        }
        return pre;
    }

    bool isPalindrome(ListNode* head) {
        if(head == nullptr) return true;
        auto i = head, j = mid_node(head)->next;
        j = reverse_list(j);
        while(i != nullptr && j != nullptr) {
            if(i->val != j->val) return false;
            i = i->next, j = j->next;
        }
        return true;
    }
};

剑指 Offer II 028. 展平多级双向链表

  1. 先递归的偏平化链表(只确定next指针)
  2. 然后再迭代一次清空child指针,重新赋值prev指针
class Solution {
public:
    Node* last_ptr(Node *head) {
        Node *cur = head, *pre = head->prev;
        while(cur != nullptr) {
            if(cur->child != nullptr) {
                auto tmp = cur->next;
                cur->next = cur->child;
                auto child_last = last_ptr(cur->child);
                child_last->next = tmp;
                pre = child_last;
                cur = tmp;
            } else {
                pre = cur;
                cur = cur->next;
            }
        }
        return pre;
    }
    Node* flatten(Node* head) {
        if(head == nullptr) return nullptr;
        last_ptr(head);
        auto cur = head , prev = head->prev;
        while(cur != nullptr) {
            cur->child = nullptr;
            cur->prev = prev;
            prev = cur;
            cur = cur -> next;
        }
        return head;
    }
};

剑指 Offer II 029. 排序的循环链表
模拟,分清楚各种情况

class Solution {
public:
    Node* insert(Node* head, int insertVal) {
        if(head == nullptr) {
            head = new Node(insertVal);
            head->next = head;
        } else {
            if(head == head->next) { //单点循环
                head->next = new Node(insertVal,head);
            } else {
                Node *cur = head->next, *pre = head;
                while(cur != head) {
									//在最大和最小之间时,insertValue是最大或最小即可插入
                    if(cur->val < pre->val && (insertVal >= pre->val || insertVal <= cur->val)) {
                        pre->next = new Node(insertVal, cur);
                        break;
                    }
									//值在cur和pre之间时可插入
                    if(insertVal >= pre->val && insertVal <= cur->val) {
                        pre->next = new Node(insertVal, cur);
                        break;
                    }
                    pre = cur;
                    cur = cur->next;
                }
							//列表中元素都相等时
                if(cur == head) { 
                    pre->next = new Node(insertVal, cur);
                }
            }
        }
        return head;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值