@forsaken said in 6-10 lines in C++:

But I'm still a little curious, is that what you normally do in real programming?

Hmm, I probably wouldn't. This ended up mostly as an exercise for using pointer pointers. Though I think they can indeed have advantages in "real" programming. I mean, Linus Torvalds seems to like them:

https://grisha.org/blog/2013/04/02/linus-on-understanding-pointers/

and how long will you pause here to check and make sure every move is what you exactly want? 10 seconds? 5 minutes?

I just spent 20 minutes to simulate on paper every possibility of the swap :D

I don't remember how long it took me and how I did it. And of course me writing it was the opposite direction of you reading it. You see my code and try to understand it. That is, you go from code to idea. Whereas I went from idea to code. I already knew what I wanted to do and just had to implement that in code. But I suspect I didn't write it directly like I showed above, not even the "original" version. More likely I started with the temporary-variable-and-four-assignments version. Which is a 3-cycle, which I know is equivalent to two swaps. And quite possibly I did draw it on paper as well. At least I did do that now in order to again understand my solution in order to explain it, because I had already forgotten what it does, i.e., forgotten the idea :-)

Oh wait, right, we can see our submission history. So, here's what my first submission looked like (and it got accepted):

ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode dummy(0), *prev = &dummy;
dummy.next = head;
for (int i=1; i<m; i++)
prev = prev->next;
ListNode *last = prev->next, *curr = last;
for (int i=m; i<=n; i++) {
ListNode *next = curr->next;
curr->next = prev->next;
prev->next = curr;
curr = next;
}
last->next = curr;
return dummy.next;
}

And my second submission was this:

ListNode* reverseBetween(ListNode *head, int m, int n) {
ListNode dummy(0), *prev = &dummy;
dummy.next = head;
for (int i=1; i<m; i++)
prev = prev->next;
ListNode *last = prev->next;
for (int i=m; i<n; i++) {
swap(prev->next, last->next->next);
swap(prev->next, last->next);
}
return dummy.next;
}

For the third submission I just renamed last to pivot and that's the "original one" I posted above.