adplus-dvertising
frame-decoration

Question

Which of the following performs deletion of the last element in the list? Given below is the Node class.
class Node
{
 protected Node next;
 protected Object ele;
 Node(Object e,Node n)
 {
  ele = e;
  next = n;
 }
 public void setNext(Node n)
 {
  next = n;
 }
 public void setEle(Object e)
 {
  ele = e;
 }
 public Node getNext()
 {
  return next;
 }
 public Object getEle()
 {
  return ele;
 }
}
class SLL 
{
 Node head;
 int size;
 SLL()
 {
  size = 0;
 }
}

a)

public Node removeLast()
{
 if(size == 0)
 return null;
 Node cur;
 Node temp;
 cur = head;
 while(cur.getNext() != null)
 {
   temp = cur;
   cur = cur.getNext();
        }
 temp.setNext(null);
 size--;
 return cur;
}

b)

public void removeLast()
{
 if(size == 0)
 return null;
 Node cur;
 Node temp;
 cur = head;
 while(cur != null)
 {
  temp = cur;
  cur = cur.getNext();
        }
 temp.setNext(null);
 return cur;
}

c)

public void removeLast()
{
 if(size == 0)
     return null;
 Node cur;
 Node temp;
 cur = head;
 while(cur != null)
 {
  cur = cur.getNext();
  temp = cur;
  }
 temp.setNext(null);
 return cur;
}

d)

public void removeLast()
{
 if(size == 0)
  return null;
 Node cur;
 Node temp;
 cur = head;
 while(cur.getNext() != null)
 {
  cur = cur.getNext();
  temp = cur;
 }
 temp.setNext(null);
 return cur;
}

a.

a

b.

b

c.

c

d.

d

Answer: (a).a

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. Which of the following performs deletion of the last element in the list? Given below is the Node class.

Similar Questions

Discover Related MCQs

Q. What is the space complexity for deleting a linked list?

Q. Which of these is an application of linked lists?

Q. What differentiates a circular linked list from a normal linked list?

Q. What is the time complexity of searching for an element in a circular linked list?

Q. Which of the following application makes use of a circular linked list?

Q. Which of the following is false about a circular linked list?

Q. Consider a small circular linked list. How to detect the presence of cycles in this list effectively?

Q. What is a skip list?

Q. Skip lists are similar to which of the following datastructure?

Q. What is the time complexity improvement of skip lists from linked lists in insertion and deletion?

Q. To which datastructure are skip lists similar to in terms of time complexities in worst and best cases?

Q. The nodes in a skip list may have many forward references. their number is determined

Q. Are the below statements true about skiplists?
In a sorted set of elements skip lists can implement the below operations

i.given a element find closest element to the given value in the sorted set in O(logn)

ii.find the number of elements in the set whose values fall a given range in O(logn)

Q. How to maintain multi-level skip list properties when insertions and deletions are done?

Q. Is a skip list like balanced tree?

Q. What is indexed skip list?

Q. What is xor linked list ?

Q. What does a xor linked list have ?

Q. What does first and last nodes of a xor linked lists contain ? (let address of first and last be A and B)

Q. Disadvantages of xor lists