Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

divide a linked list into three parts based on their position mod 3.

int split_list(struct node *ll)
{
    struct node *l1, *l2, *l3;
    if(!(ll && ll->next && ll->next->next)){
        printf("Need atleast 3 elements
");
        return -1;
    }
    l1 = ll;
    l2 = ll->next;
    l3 = ll->next->next;
    while(l3 && l3->next && l3->next->next){
        l1 = l1->next;
        l2 = l2->next->next;
        l3 = l3->next->next->next;
    }
    l3 = l2->next;
    l2->next = NULL;
    l2 = l1->next;  
    l1->next = NULL;
    l1 = ll;
    printf("l1:%d, l2=%d, l3=%d
", l1->data, l2->data, l3->data);

    return 0;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #divide #linked #list #parts #based #position #mod
ADD COMMENT
Topic
Name
2+4 =