Given a list in C:
struct listNode
{
int val;
struct listNode *nextPtr;
};
typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;
If, in the process of inserting a new item in a list given by a pointer to the last item (*sPtr
), so a new node, I want to modify the pointer *sPtr
make it point to the new node, and then make the new node the last one, it is right to write this like below?
ListNodePtr newPtr;
newPtr=malloc(sizeof(ListNode));
if(newPtr!=NULL)
{
newPtr->val=whatever;
newPtr->nextPtr=NULL;
*sPtr->nextPtr=newPtr;
*sPtr=*sPtr->nextPtr;
}