SLL: Insertion At Head Give the statements to implement an insertion at the head of a linked list as shown below. class Node<T> { private Node<T> next; private T data; Node(T data) { this.data = data; } Node<T> getNext() { return next; } T getData() { return data; } } public void insertAtHead(T data) { Node<T> node = new Node<T>(data); // A: statement to achieve action A in the picture below // B: statement to achieve action B in the picture below ... } 1: A 2: B匹配题

A
head.next = node;
B
head = node ;
C
node = head;
D
head.next = head;
E
node.next = node;
F
node.next = head;
登录即可查看完整答案
我们收录了全球超50000道真实原题与详细解析,现在登录,立即获得答案。
类似问题
SLL-InsertAfter Which XXX completes the following algorithm for inserting a new node into a singly-linked list? ListInsertAfter(list, curNode, newNode) { if (list⇢head == null) { list⇢head = newNode list⇢tail = newNode } else if (curNode == list⇢tail) { list⇢tail⇢next = newNode list⇢tail = newNode } else { XXX } } -- a.newNode⇢next = curNode⇢next; curNode⇢next = null; --- b.newNode⇢next = null; curNode⇢next = list⇢tail⇢next; --- c.newNode⇢next = curNode⇢next; curNode⇢next = newNode; --- d.newNode⇢next = curNode⇢next; curNode = newNode;
LinkedList (insertAt) Consider the following LinkedList class and Code Snippet. The method insertAt(int toInsert, int local) creates a new node containing the data toInsert and inserts it into the linked list in the correct location. If an element already exists at that index, the remaining part of the LinkedList is shifted to the right to make room for the new element. Suppose my initial LinkedList is as follows public class LinkedListInt { private static class Node{ int data; Node next; public Node(int data,Node next) { this.data = data; this.next = next; } } private Node head; private Node tail; public void insertAt(int toInsert, int local) { int count = 0; Node current = head; while(current!=null) { if(count==local-1) { Node n = new Node(toInsert,current.next); current.next = n; return; } current = current.next; count++; } }} What does the linked list look like after the call insertAt(5,1)? Answer: [Fill in the blank] (in the format of x,x,x, ... ,x without any spaces.) Using the original LinkedList {2,3,5,7}, after the call insertAt(1,4) what does the linked list look like ? Answer: [Fill in the blank] (in the format of x,x,x, ... ,x without any spaces.) what is the value of the tail instance variable ? Answer: [Fill in the blank] (a number) (y/n) Is the tail instance variable updated correctly? [Fill in the blank] (fill in y or n) Using the original LinkedList, what does the linked list look like after I call insertAt(6,0)? (y/n) List is unchanged. The new value 6 is never inserted. [Fill in the blank] Using the original LinkedList {2,3,5,7}, what does the linked list look like after I call insertAt(9,7)? Answer: [Fill in the blank] (in the format of x,x,x, ... ,x without any spaces.)
Insertion At Head: Corner Case Consider the implementation of the method InsertAtHead() of the class LinkedList. Fill in the blanks. Vanilla case. Corner Case where the list is empty before insertion. After insertion public class LinkedList<T> { private Node<T> head; private Node<T> tail; public void insertAtHead(T data) {…} public void appendAtTail(T data) {…} public int size() {…} public void delete(T data) {…} class Node {…} } public void insertAtHead(T data) { Node<T> node = new Node<T>(data); ___A___; // vanilla case ___B___; // vanilla case if (___C___) // corner case where the list is empty ___D___ // } 1: ___A___ 2: ___B___ 3: ___C___ 4: ___D___
What is the correct way to insert a new node x at the beginning of a linked list?
更多留学生实用工具
希望你的学习变得更简单
加入我们,立即解锁 海量真题 与 独家解析,让复习快人一步!