简体   繁体   中英

extractLessThan operation on a singly-linked list with no tail pointer

On a recent test this was a programming question that was asked. I originally did not answer it correctly but I have edited my original solution and posted my new updated answer below the question. I was wondering if I am heading in the correct direction with my solution or does my logic not make sense?

Implement an extractLessThan operation on a singly-linked list with no tail pointer. Your code SHOULD NOT need to delete memory. Your code must not call other LinkedList functions. Order of the extracted nodes does not matter.

    struct LinkNode {
     Data * data; // note that you can compare by calling data->compareTo(other)
     LinkNode * next;
    };
    class LinkedList {
     LinkNode * head;
     /**
     * Returns a new LinkedList that contains all of the LinkNodes from this
     * LinkedList that has node->data->compareTo(value).
     * LinkedList * x = ... // x contains [5, 8, 1, 3]
     * Data * value = new IntegerData(4);
     * LinkedList * y = x->extractLessThan(value);
     * // x now contains [5, 8] and y now contains [3, 1]
     */

    // You have access to head and to this
    LinkedList * extractLessThan(Data * value) {
     LinkedList * newList = new LinkedList();
     LinkNode * current = head;
     LinkNode * previous = NULL;



    *-----------------------MY SOLUTION---------------------------------->

    while(current){
     if(current->data->compareTo(value) < 0){
       newList->head = current;
       current = current->next;
       return extractLessThan(value);
     else {return;}
    }

No, not even close. You can't take nodes from another list and plug them into yours and somehow think you're done -- you're destroying the original list. You can only copy the values, but each node has to be new and belonging to the new list.

Also I'm not sure why you're returning extractLessThan(value) . Even assuming this is the function and your dog just ate the definition, and ignoring the fact that you later return nothing, there's no need for recursion here. You already advanced to the next node: current=current->next; .

LinkedList *newList = new LinkedList();
LinkNode *newListHead;
LinkNode *prev;
LinkNode *current = head;  
LinkNode *next;
if (head->data->compareTo(value) < 0) {
    head = head->next;
    newList->head = current;
    newListHead = current;
} 
prev = current;
current = current->next;
while(current != NULL){
    if(current->data->compareTo(value) < 0){
        next = current->next;
        if (newList->head == NULL) {
            newList->head = current;
            newListHead = current;
        } else {
            newListHead->next = current;
            newListHead = newListhead->next;
        }
        current = next;
        prev->next = next;
    } else {
        prev = current;
        current = current->next;
    }
}
return newList;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM