I'm a newbie of programming. I have wrote a function to scan inputs to linked list. But it does not work. Can anyone help me find what the problem is?
ListNode *BuildList() {
char discard;
ListNode *list,*list2=NULL;
list = (ListNode*)malloc(sizeof(struct ListNode));
if ((scanf("%d%1[^\n]s", &list->val, &discard)) == 2) {
list->next = BuildList();
printf("%d ", list->next->val);
}
else
{
list->next = NULL;
}
return list;
}
and ListNode is defined as
struct ListNode {
int val;
ListNode *next;
};
Thank you!