leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2487.cpp (658B)


      1 class Solution {
      2     ListNode *rev(ListNode *list) {
      3         ListNode *prev = nullptr, *next;
      4         while (list) {
      5             next = list->next;
      6             list->next = prev;
      7             prev = list;
      8             list = next;
      9         }
     10         return prev;
     11     }
     12 
     13   public:
     14     ListNode *removeNodes(ListNode *head) {
     15         ListNode dummy(-1, rev(head));
     16         int maxi = -1;
     17         for (ListNode *crnt = &dummy; crnt && crnt->next; crnt = crnt->next) {
     18             maxi = max(maxi, crnt->val);
     19             while (crnt->next && crnt->next->val < maxi)
     20                 crnt->next = crnt->next->next;
     21         }
     22         return rev(dummy.next);
     23     }
     24 };