K reverse linked list Time : Space : 12345678910111213141516171819202122232425262728293031323334353637383940/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ListNode* cut(ListNode* A, int k) { ListNode* runner = A; for(int i = 0; i < k - 1; i++) runner = runner->next; return runner;}ListNode* reverse(ListNode* A) { ListNode* dummy = new ListNode(-1); ListNode* runner = A; while(runner) { ListNode* next = runner->next; runner->next = dummy->next; dummy->next = runner; runner = next; } return dummy->next;}ListNode* Solution::reverseList(ListNode* A, int B) { ListNode* dummy = new ListNode(-1); ListNode* tail = dummy; ListNode* runner = A; while(runner) { ListNode* last = cut(runner, B); ListNode* next = last->next; last->next = NULL; ListNode* rev = reverse(runner); tail->next = rev; tail = runner; runner = next; } return dummy->next;}