Question

In: Computer Science

Implement a CircularDoubleLinkedList.The removelast method should be implemented with O(1) complexity. There rest is just to...

Implement a CircularDoubleLinkedList.The removelast method should be implemented with O(1) complexity. There rest is just to keep all pointers correct. (Implement in Java pls)

Solutions

Expert Solution

With a time complexity of O(n)

and space complexity of O(1)

class remLast { 
    static class Node { 
        int data; 
        Node next; 
    }; 
  
    static Node remLNode(Node head) 
    { 
        if (head == null) 
            return null; 
  
        if (head.next == null) { 
            return null; 
        } 
  
      
        Node sec_Last = head; 
        while (sec_Last.next.next != null) 
            sec_Last = sec_Last.next; 
  
       
        sec_Last.next = null; 
  
        return head; 
    } 
    static Node push(Node head_Ref, int new_Data) 
    { 
        Node new_Node = new Node(); 
        new_Node.data = new_Data; 
        new_Node.next = (head_Ref); 
        (head_Ref) = new_Node; 
        return head_Ref; 
    } 
  
    // driver code 
    public static void main(String args[]) 
    { 
        Node head = null; 
        head = push(head, 12); 
        head = push(head, 29); 
        head = push(head, 11); 
        head = push(head, 23); 
        head = push(head, 8); 
  
        head = remLNode(head); 
        for (Node temp = head; temp != null; temp = temp.next) 
            System.out.print(temp.data + " "); 
    } 
}

Related Solutions

Q1. A. What is the complexity of partition process in quick sort? O(1) O(logN) O(N) O(NlogN)...
Q1. A. What is the complexity of partition process in quick sort? O(1) O(logN) O(N) O(NlogN) B. Evaluate the following postfix expression. 2 3 4 + * C. In an array representation of heap, what are the parent node of a node a[10]? a[9] a[11] a[5] a[20] There is no easy way to access parent node in such representation. D. In an array representation of heap, what are the children nodes (if any) of a node a[10]? a[11] and a[12]...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java program to reverse a stack of characters. Make your own assumption and your own design on the methods signature, inputs, outputs, and the full main program.
discussing experience with dealing Dynamic complexity, with describing the situation and the solution that was implemented
discussing experience with dealing Dynamic complexity, with describing the situation and the solution that was implemented
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 +...
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 + n)(n ^2 + π/2 ) 3. 1 + 2 + 3 + · · · + n − 1 + n
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of the method push, pop, top, isEmpty, and size. You should not use any extra data structure. Related codes: public interface Stack<E> { int size( ); boolean isEmpty( ); void push(E e); E top( ); E pop( ); } public class LinkedStack<E> implements Stack<E> { private SinglyLinkedList<E> list = new SinglyLinkedList<>( );    public LinkedStack( ) { }    public int size( ) { return...
Implement 2-D peak finder and explain the asymptotic complexity of the algorithm For Design and Analysis...
Implement 2-D peak finder and explain the asymptotic complexity of the algorithm For Design and Analysis of the Algorithme lecture
how do i find a peak in time complexity of O(log(n)) in a list? input: a...
how do i find a peak in time complexity of O(log(n)) in a list? input: a list of numbers or integers output: a possible local peak in the list for an example: calling function [2,3,8,9,5] would return 3 im thinking about using binary search but it would require a sorted list.
An observer (O) is standing on a long tunnel that has a rest length of 12...
An observer (O) is standing on a long tunnel that has a rest length of 12 c.µs.  O sees a long train that he measures to be 12c.µstravel through the tunnel at a speed of 0.600c. The front and back of the tunnel are mounted with strobe lights so that when the front or end of the train reaches the end of the tunnel the light flashes.  So we have 4 events (flashes). Event 1:  Front of train enters tunnel. Event 2:  Front of...
PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two...
PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two lines to explain your answer. 1a int a = 0, b = 0; for (int i = 0; i< N; i++) {     a = a + i; } for (int j = 0; j < N; j++) {     b = b + j; } 1b bool isPrime(int N) { if (N == 1) return false; for (x = 2; x <= sqrt(N);...
1. What do you think about the concept of a living wage? Should it be implemented...
1. What do you think about the concept of a living wage? Should it be implemented or not? Why? 2. What should the government do to address the current unemployment crisis?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT