leetcode

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

0025.cpp (561B)


      1 class Solution {
      2   public:
      3     ListNode *reverseKGroup(ListNode *head, int k) {
      4         stack<ListNode *> st;
      5         ListNode *tmp, *dummy, *next;
      6 
      7         tmp = dummy = new ListNode(-1, nullptr);
      8         for (ListNode *p = head; p; p = next) {
      9             next = p->next;
     10             st.push(p);
     11             if (st.size() == k) {
     12                 while (!st.empty()) {
     13                     tmp = tmp->next = st.top();
     14                     st.pop();
     15                 }
     16                 tmp->next = next;
     17             }
     18         }
     19         return dummy->next;
     20     }
     21 };