Thread: Inserting a node at the tail of singly linked list

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    82

    Inserting a node at the tail of singly linked list

    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.



    Last edited by ghoul; 10-23-2020 at 01:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-02-2016, 06:44 AM
  2. Delettion of node in singly linked list
    By gamer_trex29 in forum C Programming
    Replies: 7
    Last Post: 06-12-2007, 02:00 PM
  3. Problems with deleting a node in a singly linked list
    By omishompi in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2006, 06:27 PM
  4. Deleting node from singly linked list
    By Happy Man in forum C Programming
    Replies: 2
    Last Post: 04-16-2004, 06:56 AM
  5. Connecting node in singly linked list
    By hmaya74 in forum C Programming
    Replies: 1
    Last Post: 03-16-2002, 09:50 AM

Tags for this Thread