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
  • 2) (10 pts) Write a function that takes in a pointer to a linked list of...

    2) (10 pts) Write a function that takes in a pointer to a linked list of nodes storing integers and a variable named value, and returns the number of nodes in the list storing that value. For example, if a list pointed to by listPtr stores 2, 6, 2, 3, 4, 2, 6, and 6 and value = 6, your function should return 3, since 6 appears in the list 3 times. Please use the struct and function prototype provided...

  • 1) (10 pts) Write a recursive function that counts and returns the number of nodes in...

    1) (10 pts) Write a recursive function that counts and returns the number of nodes in a binary tree with the root root, that store an even value. Please use the struct shown and function prototype shown below. (For example, if the tree rooted at root stored 2, 3, 4, 8, 13, 18 and 20, the function should return 5, since there are five even values [2,4,8,18,20] stored in the tree. typedef struct node { int data; struct node* left;...

  • Question 2.)Let A= student id last digit % 4 and add 1. For example mine student...

    Question 2.)Let A= student id last digit % 4 and add 1. For example mine student id is 7. So 7%4=3 and add 1 -->A=4 Run DFS starting at node a, and breaking ties alphabetically(in this case numerically as nodes have numbers where 1 goes before 2). Please label just back edges. 3 2 6 2 8 1 3 5 4 3

  • 4. [10 pts] Let X be a random variable with probability density function if 1 <...

    4. [10 pts] Let X be a random variable with probability density function if 1 < a < 2, 2 f(a)a 0 otherwise. Find E(log X). Note: Throughout this course, log = loge.

  • Score on last try: 0,5 of 1 pts. See Details for more. > Next question You...

    Score on last try: 0,5 of 1 pts. See Details for more. > Next question You can retry this question below Graphing Linear Functions Identify the Slope and Intercepts of the function f(x) function. = 3z. Then draw an accurate graph of the T5 10- f(z) = 3 %3D Slope 15 -10 10 y-intercept x-intercept -10- 15+ Clear All Draw: Question Help: D Video Submit Question

  • 1. [10 pts.] AVL Trees: Example Operations (a) [5 pts.] Draw the AVL tree that results...

    1. [10 pts.] AVL Trees: Example Operations (a) [5 pts.] Draw the AVL tree that results from inserting key 45 into the following AVL tree. (b) [5 pts.] Draw the AVL tree that results from deleting key 70 from the following AVL tree. NOTE: When deleting a key from an AVL tree, please follow the textbook approach of finding the node with the key using the function for standard binary search trees. If the key is in the tree and...

  • For Python 3 MPLS please 1. Given a variable, unproved_conjectures, that is associated with a dictionary...

    For Python 3 MPLS please 1. Given a variable, unproved_conjectures, that is associated with a dictionary that maps the common names of mathematical conjectures to the years when the conjectures were made, write a statement that deletes the entry for "Fermat's Last Theorem". 2. Given a dictionary d, create a new dictionary that reverses the keys and values of d. Thus, the keys of d become the values of the new dictionary and the values of d become the keys...

  • This Question: 2 pts 1 of 4 (0 complete) The function graphed is of the form...

    This Question: 2 pts 1 of 4 (0 complete) The function graphed is of the form y- a sin bx or y a cos bx, where b> 0. Determine the equation of the graph. y(Use integers or fractions for any numbers in the expression.)

  • 3-) Let ocr<1 o w UUUUU is probability destiny function of X random variable. a- )...

    3-) Let ocr<1 o w UUUUU is probability destiny function of X random variable. a- ) Find PlOCXCI) b.) Find Pix > 15) UUUUUU ca) Find € (x) and Var(x) d-) Find the distribution function

  • 1. static int function(int n){ return n * function(n + 1); } If you call the...

    1. static int function(int n){ return n * function(n + 1); } If you call the method function(2), the expected output is 3. True False 2. public void push(int new_data) { Node new_Node = new Node(new_data); new_Node.next = head; new_Node.prev = null; if (head != null) head.prev = new_Node; head = new_Node; } The code snippet depicts stack implementation True False 3. A ________ list is a linked data structure that consists of a set of sequentially linked records called...

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