g 2 > 3 > 1 > 7 > 5 > 18 > null here the > symbol means a pointer. write a complete java program which deletes the third last node of the list and returns to you the list as below 2 > 3 > 1 > 5 > 18 > null the third last node (with a value 7) has now been dislodged from the list. here are the few things to consider : (a) we do not know the length of the list. (b) your solution should be in o(n) time and o(1) space having made just a single pass through the list. any solution that makes more than one pass through the list to delete the required node would only receive half credit.

Respuesta :

Using the knowledge of computational language in JAVA it is possible to write a code that program which deletes the third last node of the list and returns to you the list as below 2 > 3 > 1 > 5 > 18 > null the third last node.

Writting the code:

class LinkedList {

   Node head;

   class Node {

       int data;

       Node next;

       Node(int d)

       {

           data = d;

           next = null;

       }

   }

 //function to get the nth node from end in LL

   int NthFromLast(int n)

   {

       int len = 0;

       Node temp = head;

     //length of LL

       while (temp != null) {

           temp = temp.next;

           len++;

       }

      //check if the asked position is not greater than len of LL

       if (len < n)

           return -1;

       temp = head;

       for (int i = 1; i < len - n + 1; i++)

           temp = temp.next;

       return(temp.data);

   }

 

  //function to delete a node with given value

   void deleteNode(int key)

   {

       Node temp = head, prev = null;

       // If node to be deleted is at head

       if (temp != null && temp.data == key) {

           head = temp.next;

           return;

       }

       // Search for the key to be deleted

       while (temp != null && temp.data != key) {

           prev = temp;

           temp = temp.next;

       }

       // If key not present in linked list

       if (temp == null)

           return;

       prev.next = temp.next;

   }

   //function to insert a new node in LL

   public void push(int new_data)

   {

       Node new_node = new Node(new_data);

       new_node.next = head;

       head = new_node;

   }

   //function to print the LL

   public void printList()

   {

       Node tnode = head;

       while (tnode != null) {

           System.out.print(tnode.data + " ");

           tnode = tnode.next;

       }

   }

   //driver method

   public static void main(String[] args)

   {

       LinkedList llist = new LinkedList();

       llist.push(18);

       llist.push(5);

       llist.push(7);

       llist.push(1);

        llist.push(3);

         llist.push(2);

       System.out.println("\nCreated Linked list is:");

       llist.printList();

       llist.deleteNode(llist.NthFromLast(3)); //delete the third                                      //last node

       System.out.println(

           "\nLinked List after Deletion of third last:");

       llist.printList();

   }

}

See more about JAVA at brainly.com/question/18502436

#SPJ1

Ver imagen lhmarianateixeira