Suppose we are considering a doubly linked list which has at least 2 nodes. Select the most correct java code snippet that deletes the last node.(prev is a link to a predecessor node)
4 phương ánCó giải thích
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "Node f = tail.prev; f.next = null; tail = f;". Trong danh sách liên kết đôi, tail.prev cho ngay nút kế cuối mà không cần đi từ head, nên phép gán f = tail.prev là O(1); f.next = null cắt liên kết xuôi tới nút cuối cũ và tail = f dời đuôi về f, nhờ đó nút cuối cũ không còn ai tham chiếu và cả hai mốc head, tail đều nhất quán. Phương án "Node f = tail.prev; f.next = null;" cắt đúng nhưng bỏ quên tail, nên tail vẫn trỏ vào nút vừa bị loại: đi xuôi từ head thì không thấy nút cuối mà biến tail lại khẳng định nó còn đó. Phương án "Node f = tail.prev; f.next = tail; tail = f;" gán lại chính liên kết cũ rồi mới dời tail, kết quả là nút cuối vẫn nằm trong danh sách trong khi tail chỉ vào nút kế cuối, không nút nào bị xoá. Phương án "Node f = tail.prev; f.next = tail.next; tail = f.next;" lấy tail.next vốn bằng null nên f.next = null là đúng, nhưng ngay sau đó tail = f.next lại đặt tail bằng null, làm mất hẳn đuôi danh sách dù các nút phía trước vẫn còn.
Cùng phần 1
Suppose a doubly linked list of integers is given below and p is a reference to the node with value 30 in the list (i.e. p.info=30):
(head) 7 11 6 4 30 9 8 2 (tail)
What does the list look like after the following java code snippet is run?
Node p1,p2; p1 = p.next;
p2 = p1.next;
p.next = p2;
if(p2!=null) p2.prev=p;Có lời giảiCâu hỏiSuppose a doubly linked list of integers is given below and p is a reference to the node with value 13 in the list (i.e. p.info=13):
(head) 7 11 6 4 32 13 8 2 (tail)
What does the list look like after the following java code snippet is run?
int x = 17;
Node q = new Node(x);
q.prev=null;
q.next = head;
head.prev = q;
head = q;Có lời giảiCâu hỏiSelect the statement that is most correct.
Basically, the complexity of getting an element at the middle of a singly linked lists isCó lời giảiCâu hỏiCả 64 câu của chủ đề64 câu
Xuất hiện trong 1 bộ đề
Đi tiếp từ đây