leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0025.cpp (561B)
0 class Solution { 1 public: 2 ListNode *reverseKGroup(ListNode *head, int k) { 3 stack<ListNode *> st; 4 ListNode *tmp, *dummy, *next; 5 6 tmp = dummy = new ListNode(-1, nullptr); 7 for (ListNode *p = head; p; p = next) { 8 next = p->next; 9 st.push(p); 10 if (st.size() == k) { 11 while (!st.empty()) { 12 tmp = tmp->next = st.top(); 13 st.pop(); 14 } 15 tmp->next = next; 16 } 17 } 18 return dummy->next; 19 } 20 };