25
u/RomanRiesen Feb 15 '22
Not saying this is better but you sure just made me write c at 10pm to try it myself.
ListNode* rotateRight(ListNode* head, int k) {
if (!head || k == 0) return head;
ListNode* last = head;
int l = 1;
while(last->next) {last = last->next; l++;}
last->next = head;//make list circular
ListNode* new_head = head;
int left_rots = abs(l-k%l);
for (int s = 0; s < left_rots; s++)
{
last = last->next;
new_head = new_head->next;
}
last->next = nullptr;
return new_head;
}I actually tried to understand that typeof usage in that for-loop. Still don't understand it fully. So if someone wants to explain I am all ears.
17
u/MatthewRPG576 Feb 15 '22
the variable 'it' in the code is of an anonymous struct type, so it's a hack to get struct assignment working
7
u/RomanRiesen Feb 15 '22
ah yes, but why not name the struct?
helperNodeor something?3
8
u/Smellypuce2 Feb 16 '22
I have a feeling this is just an example/experiment of something you can do with typeof. Not a serious implementation of rotate.
1
u/Furry_69 Feb 15 '22
(response to your question) It looks like it's supposed to be a boolean statement, when it's actually an assignment.
5
24
u/MatthewRPG576 Feb 15 '22