How Would You Delete A Node In The Singly Linked List The #78
How would you delete a node in the singly linked list? The position to be deleted is given.
This multiple choice question (MCQ) is related to the book/course gs gs121 Data Structures and Algorithms. It can also be found in gs gs121 Abstract Data Types - Singly Linked Lists - Quiz No.1.
How would you delete a node in the singly linked list? The position to be deleted is given.
public void delete(int pos) { if(pos < 0) pos = 0; if(pos > size) pos = size; if( size == 0) return; if(pos == 0) head = head.getNext(); else { Node temp = head; for(int i=1; i<pos; i++) { temp = temp.getNext(); } temp.setNext(temp.getNext().getNext()); } size--; }
public void delete(int pos) { if(pos < 0) pos = 0; if(pos > size) pos = size; if( size == 0) return; if(pos == 0) head = head.getNext(); else { Node temp = head; for(int i=1; i<pos; i++) { temp = temp.getNext(); } temp.setNext(temp.getNext()); } size--; }
public void delete(int pos) { if(pos < 0) pos = 0; if(pos > size) pos = size; if( size == 0) return; if(pos == 0) head = head.getNext(); else { Node temp = head; for(int i=1; i<pos; i++) { temp = temp.getNext().getNext(); } temp.setNext(temp.getNext().getNext()); } size--; }
public void delete(int pos) { if(pos < 0) pos = 0; if(pos > size) pos = size; if( size == 0) return; if(pos == 0) head = head.getNext(); else { Node temp = head; for(int i=0; i<pos; i++) { temp = temp.getNext(); } temp.setNext(temp.getNext().getNext()); } size--; }
Similar question(s) are as followings:
Online Quizzes of gs121 Data Structures and Algorithms
Binary Trees - Binary Search Tree - Quiz No.1
gs gs121 Data Structures and Algorithms
Online Quizzes
Binary Trees - Binary Search Tree - Quiz No.2
gs gs121 Data Structures and Algorithms
Online Quizzes
Binary Trees - Preorder Traversal - Quiz No.1
gs gs121 Data Structures and Algorithms
Online Quizzes