Question

SLL, 25 pts] (back variable is NOT available) (10 pts) Introduce a function that deletes last node. If no node available, do nothing. (15 pts) Introduce a function that deletes all duplicate nodes. For example, 1- 1->1-3->2->4>3->2->1 will become 1->3->2->4 a. b.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

a.) function that deletes last node.

public ListNode deleteLast(ListNode head)
{
    if(head == NULL)   //if Linked List is empty then Null is return
        return head;

    if(head->next == NULL)   // if linked list has only one element, then head is deleted
    {
        head = NULL;
        return head;
    }

    ListNode* currNode = head;
    while (currNode->next && currNode->next->next != NULL)   //while loop finds the last element in Linked List
    {
        currNode = currNode->next;
    }
    currNode->next = NULL;
    return head;
}

B.) function that delete all duplicate nodes.

// the working of the function is same as bubble sort, we will compare each node value with every node in Linked List and if we find the duplicate then we will delete it

void delete_duplicates(struct LinkedList *head)
{
    struct LinkedList *ptr1, *ptr2, *dup;
    ptr1 = head;

    /* Pick elements one by one */
    while (ptr1 != NULL && ptr1->next != NULL)
    {
        ptr2 = ptr1;

        /* while loop to Compare the elements */
        while (ptr2->next != NULL)
        {
            /* If duplicate then delete it */
            if (ptr1->data == ptr2->next->data)
            {
                dup = ptr2->next;
                ptr2->next = ptr2->next->next;
                delete(dup);
            }
            else
                ptr2 = ptr2->next;
        }
        ptr1 = ptr1->next;
    }
}

Add a comment
Know the answer?
Add Answer to:
SLL, 25 pts] (back variable is NOT available) (10 pts) Introduce a function that deletes last...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT