英文:
inserting a node at the end of a singly linked list in java geeksforgeeks
问题
这是我从geeksforgeeks上获得的关于在单链表末尾插入节点的代码。我不理解第四步。为什么将new_node.next设为null,当在创建new_node时它本应该是null,因为它没有被初始化?
// 链表类
class LinkedList
{
Node head; // 链表头节点
/* 节点类 */
class Node
{
int data;
Node next;
// 构造函数创建新节点
Node(int d) {data = d; next = null; }
}
/* 在末尾添加新节点。此方法在上面的LinkedList类中定义 */
public void append(int new_data)
{
/* 1. 分配节点 &
2. 放入数据
3. 将next设置为null */
Node new_node = new Node(new_data);
/* 4. 如果链表为空,则将新节点作为头节点 */
if (head == null)
{
head = new Node(new_data);
return;
}
/* 4. 这个新节点将成为最后一个节点,所以将其next设为null */
new_node.next = null;
/* 5. 否则遍历到最后一个节点 */
Node last = head;
while (last.next != null)
last = last.next;
/* 6. 修改最后一个节点的next */
last.next = new_node;
return;
}
}
英文:
This is a code I got from geeksforgeeks on inserting a node at the end of a singly linked list. I do not understand step four. Why is it making the new_node.next into null, when it should be null in the first place when it was not initialized when the new_node was created?
// Linked List Class
class LinkedList
{
Node head; // head of list
/* Node Class */
class Node
{
int data;
Node next;
// Constructor to create a new node
Node(int d) {data = d; next = null; }
}
/* Appends a new node at the end. This method is
defined inside LinkedList class shown above */
public void append(int new_data)
{
/* 1. Allocate the Node &
2. Put in the data
3. Set next as null */
Node new_node = new Node(new_data);
/* 4. If the Linked List is empty, then make the
new node as head */
if (head == null)
{
head = new Node(new_data);
return;
}
/* 4. This new node is going to be the last node, so
make next of it as null */
new_node.next = null;
/* 5. Else traverse till the last node */
Node last = head;
while (last.next != null)
last = last.next;
/* 6. Change the next of last node */
last.next = new_node;
return;
}
}
答案1
得分: 0
是的,这一行代码:
new_node.next = null;
是不必要的。实际上,即使是注释也证明了这一点。步骤#3的注释和第二步#4的注释解释了相同的操作,没有办法在执行了前一步骤之前执行后者。
另一个不必要的步骤,一个更重要的步骤,最先被 @DaveNewton 发现,但我错过了。这一行代码:
head = new Node(new_data);
当列表为空时,应该是:
head = new_node;
以防止额外无用的分配 Node
对象。选择性地,这一行代码:
Node new_node = new Node(new_data);
可以移动到 if
代码块下面,但这将不必要地重复代码(但不会增加工作量)。
代码末尾的 return
语句也是不必要的。
英文:
Yes, the line:
new_node.next = null;
is unnecessary. In fact, even the comments bear witness to this. The step #3 comment and the second step #4 comment explain the same operation, and there's no way to do the latter without having done the former.
Another unnecessary step, a much more significant one, was noticed first by @DaveNewton and missed by me. The line:
head = new Node(new_data);
that occurs when the list is empty, should be:
head = new_node;
preventing an extra useless allocation of a Node
object. Optionally, the line:
Node new_node = new Node(new_data);
could be moved below the if
block, but that would duplicate code unnecessarily (but not effort).
The return
statement at the very end of the code is also unnecessary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论