[C] Linked list

typedef struct node {
int val;
struct node* next;
} Node;
Node* create_list(int arr[], int len){
//boundary
if(!arr || len == 0)
return NULL;

//main
Node *head, *tail;
for(int i=0; i<len; i++){
//create node
Node* curr = (Node*)malloc(sizeof(Node));
curr->val = arr[i];
curr->next = NULL;
//if it's first new node
if(i == 0){
head = curr;
tail = curr;
}else{
tail->next = curr;
tail = tail->next;
}
}

return head;
}
void print_list(Node* head){
Node* p = head;
while(p){
printf("%d ", p->val);
p = p->next;
}
printf("\n");
}
void delete_list(Node* head){
while(head){
Node* tmp = head;
head = head->next;
free(tmp);
}
}

--

--

Hi I am Samuel

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store