Hello,
Below is my half-witted attempt at inserting a node at the tail of a singly linked list. Assumptions are that the list is not sorted. This should probably be simple and easy to do but my lack of seriousness and focus is probably the problem. It probably makes me look a bit silly but you are better of looking silly while being smart than looking smart while being silly.
Code:
/*
* list structure
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/
SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {
SinglyLinkedListNode *new_node = (SinglyLinkedListNode*)malloc(sizeof(SinglyLinkedListNode));
new_node->data = data;
SinglyLinkedListNode *temp = head->next;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = new_node;
return head;
}
Is this heading in the right direction? Is the algorithm correct? Why is it segmentation faulting?
This is preparation for some upcoding coding challenge which might lead me to an interview for a permanent or probably semi-permanent job. This particular challenge was picked off on hackerrank.