Suppose we are considering a singly linked list and p is some node in the list which has successor node. Select the most correct java code snippet that inserts new node with value x after the node p.
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 q = new Node(x); q.next = p.next; p.next = q;". Thứ tự hai lệnh gán là điểm cốt yếu: trước hết cho nút mới nhận phần đuôi cũ bằng q.next = p.next, sau đó mới cho p trỏ sang nút mới; nhờ vậy không mắt nối nào bị mất và chi phí là hằng số. Bản "p.next = new Node(x); p=p.next;" ghi đè p.next trước khi lưu lại nút kế, nên toàn bộ phần sau p rơi khỏi danh sách. Bản "Node q = new Node(x); q.next = null; p.next = q;" biến nút mới thành nút cuối, cắt đứt mọi nút phía sau p dù đề nói rõ p có nút kế. Bản "Node q = new Node(x); p.next = q; q.next = p.next;" đảo thứ tự: sau lệnh đầu thì p.next đã là q, nên lệnh sau gán q.next = q tạo vòng tự trỏ và phần đuôi cũ mất hẳ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