I'm trying to go through a linked list and print all the alternating nodes at once. I'm not allowed to create a second list/array to store all the alternate nodes values. So far I have been able to go through the alternating nodes with the code below. However it will keep printing the value as it is looping through the nodes...
let's say the list is [1, 2, 3, 4]
NodeT *p;
NodeT *a;
p = list;
while (p != NULL)
{
printf("p is %d-->", p->data);
a = p->next;
printf("a is %d-->", a->data);
p = p->next->next;
}
The current result is: p is 1-->a is 2-->p is 3-->a is 4-->
The result that I'm looking for is:
p is 1-->3 a is 2-->4
is there any way to achieve this without creating any new list/array?