zoukankan      html  css  js  c++  java
  • 面试题24:单链表逆序

    ListNode* reverseList(ListNode* head) {
        if (head == nullptr || head->next == nullptr) return head;
        ListNode* dummy = new ListNode(-1);
        dummy->next = head;
    
        ListNode* p = head->next;
        ListNode* p_pre = head;
        while (p) {
            ListNode* p_next = p->next;
            p_pre->next = p->next;
            p->next = dummy->next;
            dummy->next = p;
            p = p_next;
        }
        return dummy->next;
    
    }
    
    ListNode *reverseBetween(ListNode *head, int m, int n) {
        if (n - m < 1) return head;
        ListNode* dummy = new ListNode(-1);
        dummy->next = head;
    
        ListNode *start = head;
        ListNode* start_pre = dummy;
        for (int i = 1; i < m; i++) {
            start_pre = start;
            start = start->next;
    
        }
        for (int i = 0; i < n - m; i++) {
            ListNode* tmp = start->next;
            start->next = start->next->next;
            tmp->next = start_pre->next;
            start_pre->next = tmp;
        }
        return dummy->next;
    }
  • 相关阅读:
    django学习笔记1
    排序多重排序
    06计算列
    填充日期序列
    行,列单元格
    读取excel文件
    监控文本
    天干地支纪年法
    Mysql基础
    JDBC基础
  • 原文地址:https://www.cnblogs.com/wxquare/p/6877058.html
Copyright © 2011-2022 走看看