2021-05-06链表

25.

public ListNode reverseKGroup(ListNode head, int k) {
    ListNode dummy = new ListNode(0);
    dummy.next = head;

    ListNode pre = dummy;
    ListNode end = dummy;

    while (end.next != null) {
        for (int i = 0; i < k && end != null; i++) end = end.next;
        if (end == null) break;
        ListNode start = pre.next;
        ListNode next = end.next;
        end.next = null;
        pre.next = reverse(start);
        start.next = next;
        pre = start;

        end = pre;
    }
    return dummy.next;
}

private ListNode reverse(ListNode head) {
    ListNode pre = null;
    ListNode curr = head;
    while (curr != null) {
        ListNode next = curr.next;
        curr.next = pre;
        pre = curr;
        curr = next;
    }
    return pre;
}

本人错解
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {//在这里它头节点不能丢,dummyhead必须要有
        ListNode* dummyHead = new ListNode;
        dummyHead->next = head;
        ListNode* start = head;
        ListNode* end = head;
        ListNode* prev = dummyHead;
        ListNode* next;
        while(end != nullptr){
            for(int i= 0; i<k; i++){
                //if(end->next == nullptr) break;
                if(end->next) end = end->next;
                else break;
            } 
            next = end->next;
            reverseK(start);
            start->next = next;
            prev->next = end;
            prev = start;
            end = prev;
        }
        return dummyHead->next;
    }

    void reverseK(ListNode* head){
        ListNode* cur = head;
        ListNode* next = cur->next;
        ListNode* prev = nullptr;
        while(cur != nullptr){
            next = cur->next;
            cur->next = prev;
            prev = cur;
            cur = next;
        }
        head = prev;
    }
};

82.

现在难题是???只能去重,shan

  • 这里的del完全无必要,cur->next完全可以满足。
  • 连=nullptr也不必要有,
  • next存下一个也不必要,cur->next一步也就到位了。
  • 分if, else两种判断
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* dummyHead = new ListNode;
        dummyHead->next = head;
        ListNode* prev = dummyHead;
        ListNode* cur = head;
        ListNode* del = head;
        ListNode* next;
        while(cur != nullptr){
            del = cur->next;
            if(del != nullptr && del->val == cur->val){//一旦发生重合??
                while(del->val == cur->val) del = del->next;
                next = del->next;
                del->next = nullptr;
                prev->next = next;
                cur = prev;
            }
            prev = cur;
            cur = cur->next;
        }
        return dummyHead->next;
    }
};

正解
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* dummyHead = new ListNode;
        dummyHead->next = head;
        ListNode* prev = dummyHead;
        ListNode* cur = head;
        ListNode* next;
        while(cur != nullptr){
            if(cur->next != nullptr && cur->val == cur->next->val){//一旦发生重合??删了还没删结果不一样
                while(cur->next  && cur->val == cur->next->val) cur = cur->next;
                next = cur->next;
                prev->next = next;
                cur = next;
            }else{
                prev = cur;
                cur = cur->next;
            }    
        }
        return dummyHead->next;
    }
};

注意使用 ,第一个判定条件一定要有!!

cur->next != nullptr && cur->val == cur->next->val
if(cur->next != nullptr && cur->val == cur->next->val)

83.

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* dummyHead = new ListNode;
        dummyHead->next = head;
        ListNode* cur = head;
        ListNode* del;
        while(cur && cur->next){
            del = cur->next;
            while(del && del->val == cur->val){
                cur->next = del->next;
                del = cur->next;
            }
            cur = cur->next;
        }
        return dummyHead->next;
    }
};
while(cur && cur->next){

 

while(del && del->val == cur->val){

143.

  • 如何节省??正向的?先做个反向的列表,再一一一一
  • 也可以装入双端队列deque?
  •  

  • 为何该方法一个也算不出???
  • 多个指针指向了同一个节点,只会报错!!!
  • int nums = (n-1)/2; 这里四只要循环1次,小细节。

class Solution {
public:
    void reorderList(ListNode* head) {
        ListNode* cur = head;
        ListNode* last = head;
        ListNode* next;
        while(cur){
            while(last && last->next) last = last->next;    //这样求出是最后一个吧??
            next = cur->next;
            cur->next = last;
            last->next = next; //据说可能多个指针指向一个??
            if(cur->next == nullptr) break;
            cur = cur->next->next;
        }
    }
};

仍错误
class Solution {
public:
    void reorderList(ListNode* head) {
        ListNode* dummyHead = new ListNode;
        dummyHead->next = head;
        vector<ListNode*> vec;
        ListNode* cur = head;
        while(cur){
            vec.push_back(cur);
            cur = cur->next;
        } 
        int n = vec.size();
        int nums = n/2;
        cur = dummyHead->next;
        for(int i=0; i<nums; i++){
            ListNode* next = cur->next;
            vec[n-1-i-1]->next = nullptr;
            cur->next = vec[n-1-i];
            vec[n-1-i]->next = next;
        }
    }
};

也错
class Solution {
public:
    void reorderList(ListNode* head) {
        ListNode* dummyHead = new ListNode;
        dummyHead->next = head;
        vector<ListNode*> vec;
        ListNode* cur = head;
        while(cur){
            vec.push_back(cur);
            cur = cur->next;
        } 
        int n = vec.size();
        int nums = n/2;
        cur = dummyHead->next;
        for(int i=0; i<nums; i++){
            vec[n-1-i-1]->next = nullptr;
            vec[i]->next = vec[n-i-1];
            vec[n-1-i]->next = vec[i+1];
        }
    }
};

正确???
class Solution {
public:
    void reorderList(ListNode* head) {
        ListNode* dummyHead = new ListNode;
        dummyHead->next = head;
        vector<ListNode*> vec;
        ListNode* cur = head;
        while(cur){
            vec.push_back(cur);
            cur = cur->next;
        } 
        int n = vec.size();
        int nums = (n-1)/2;
        cur = dummyHead->next;
        for(int i=0; i<nums; i++){
            vec[n-1-i-1]->next = nullptr;
            vec[i]->next = vec[n-i-1];
            vec[n-1-i]->next = vec[i+1];
        }
    }
};、

官方??

        int i = 0, j = vec.size() - 1;
        while (i < j) {
            vec[i]->next = vec[j];
            i++;
            if (i == j) {
                break;
            }
            vec[j]->next = vec[i];
            j--;
        }
        vec[i]->next = nullptr;
int n = vec.size();
        int nums = (n-1)/2;

 4-1 /2 = 1!!!

5-1 /2 = 2

876

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

206.

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* dummyHead = new ListNode;
        ListNode* cur = head;
        ListNode* next;
        ListNode* tail = nullptr;
        dummyHead->next = tail;
        while(cur){
            next = cur->next;
            dummyHead->next = cur;
            cur->next = tail;
            tail = cur;
            cur = next;
        }
        return dummyHead->next;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值