i am trying to make a function that changes the order of the pointers of the nodes so that the original list is reversed.
my solution is based on iterating over the main list, then reversing the order of each 2 adjacent nodes: (n1)->(n2)
would be (n1)<-(n2)
after the first iteration.
my try:
Node push1(Node* curr) {
if(curr == NULL || *curr == NULL) {
return NULL;
}
Node temp = (*curr)->next;
if(temp == NULL) {
return NULL;
}
(*curr)->next = *curr;
return temp;
}
/*******************************/
void reverse2(Node* head) {
Node curr = *head;
while(curr != NULL) {
curr = push1(&curr);
}
}
PROBLEM: i ran through an infinity loop. i tried to fix that but then the list didn't reverse order. is there a way using this approach of push1()
that could work?
NOTE: i am not seeking the solution with 3 pointers or recursion.