This expression statement that is valid in C
head,next,n1=malloc(sizeof(node));
consists of three subexpressions that are operands of the comma operator.
You can imagine it like
( head ), ( next ), ( n1=malloc(sizeof(node)) );
So as you can see only n1 is assigned with the value returned by the call of malloc
.
You could write instead
head = next = n1 = malloc(sizeof(node));
but again in this case function malloc is called once and its value is assigned to three pointers n1, next and head. That is all three pointers will have the same value - the address of the allocated memory by this one call of malloc.
So if you want that each pointer would point to its own allocated memory then you have to call malloc three times.
head = malloc(sizeof(node));
next = malloc(sizeof(node));
n1 = malloc(sizeof(node));