spider68

reorder list linked list leetcode

May 28th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. void reorderList(ListNode* head) {
  2.         if(!head||!head->next)return;
  3.         ListNode*h1=head,*h2=head->next;
  4.         while(h2&&h2->next)
  5.         {
  6.             h1=h1->next;
  7.             h2=h2->next->next;
  8.         }
  9.         h2=h1->next;
  10.         h1->next=NULL;
  11.         h1=head;
  12.         ListNode*pre=NULL;
  13.         while(h2)
  14.         {
  15.             ListNode*cur=h2->next;
  16.             h2->next=pre;
  17.             pre=h2;
  18.             h2=cur;
  19.         }
  20.         h2=pre;
  21.         while(h2)
  22.         {
  23.             pre=h1->next;
  24.             h1=h1->next=h2;
  25.             h2=pre;
  26.         }
  27.     }
Add Comment
Please, Sign In to add comment