Title:

思路:使用快慢指针,当快指针指向链表尾部时,将慢指针所指即以后反转,再将前后两个链表合并

class Solution {
public:
    void reorderList(ListNode* head) {
        ListNode*fast = head;
        ListNode*slow = head;
        while (fast && fast->next){
            slow = slow->next;
            fast = fast->next->next;
        }
        ListNode* mid = slow;
       
        ListNode* tail = NULL;
        while (slow){
            ListNode* t = slow->next;
            slow->next = tail;
            tail = slow;
            slow = t;
        }
        fast = head;
        ListNode* head1 = new ListNode(0);
        ListNode* p = head1;
        while (fast != mid && tail){
            p->next = fast;
            p = fast;
            fast = fast->next;
            p->next = tail;
            p = tail;
            tail = tail->next;
        }
        if (tail){
            p->next = tail;
        }
        head = head1->next;
    }
};

 

相关文章:

  • 2021-11-14
  • 2021-08-25
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
猜你喜欢
  • 2021-08-18
  • 2021-12-06
  • 2022-03-08
  • 2021-10-31
  • 2021-12-09
  • 2021-10-15
  • 2022-12-23
相关资源
相似解决方案