[英]implementation of a linked list in java
I was reading about linked lists in java , I did program this code , when I run it I get 3
as output , it should print out a reverse order of the inserted numbers as following : 10 7 3
.. what is wrong with my code? 我正在阅读有关Java中链表的信息,我已对此代码进行了编程,运行它时,我得到
3
作为输出,它应该按如下所示打印出插入数字的相反顺序: 10 7 3
..我的代码有什么问题?
public class List {
private class ListNode {
private int contents;
private ListNode next;
ListNode(int contents) {
this.contents = contents;
this.next = null;
}
}
private ListNode head;
public List() {
head = null;
}
public void insert(int contents) {
ListNode newNode = new ListNode(contents);
if (head == null) {
head = newNode;
}
ListNode current = head;
while (current != null) {
current = current.next;
}
current = newNode;
}
public void reverse() {
ListNode current = head;
ListNode nodeNext = current.next;
while (nodeNext != null) {
current.next = null;
nodeNext.next = current;
current = nodeNext;
}
head = current;
}
public String toString() {
String s = "";
ListNode current = head;
while (current != null) {
s = current.contents + s;
current = current.next;
}
return s;
}
public static void main(String[] args) {
List l = new List();
l.insert(3);
l.insert(7);
l.insert(10);
l.reverse();
System.out.println(l.toString());
}
}
Thanks 谢谢
Your insert
method doesn't connect the new node to the last node of the existing list. 您的
insert
方法不会将新节点连接到现有列表的最后一个节点。 You have to assign the new node to node.next
of the last node of the existing list : 您必须将新节点分配给现有列表的最后一个节点的
node.next
:
public void insert(int contents) {
ListNode newNode = new ListNode(contents);
if (head == null) {
head = newNode;
return;
}
ListNode current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
private ListNode head;
private ListNode tail;
public void insert(int contents) {
ListNode newNode = new ListNode(contents);
if (head == null) {
head = newNode;
tail = newNode;
return;
}
tail.next = newNode;
tail = newNode;
}
Keep a tail node reference for O(1) insertion. 保留用于O(1)插入的尾节点引用。
Your reverse()
method is a bit wrong: 您的
reverse()
方法有点错误:
// this loop will run forever if there are more than 1 nodes
while (nodeNext != null) {
current.next = null;
nodeNext.next = current; // you lose the reference to the entire list here
current = nodeNext;
}
Rewrite the function: 重写函数:
public void reverse() {
ListNode cursor = head;
ListNode newHead = null;
while (cursor != null) {
ListNode next = cursor.next;
cursor.next = newHead;
newHead = cursor;
cursor = next;
}
head = newHead;
}
Doing cursor.next = newHead
, loses the original reference to cursor.next
. 这样做
cursor.next = newHead
,失去原有的参考cursor.next
。 And so you need to take the reference of cursor.next
in a temporary variable: ListNode next = cursor.next;
所以你需要采取的参考
cursor.next
在临时变量: ListNode next = cursor.next;
A print function 打印功能
public void print() {
ListNode cursor = head;
while (cursor != null) {
System.out.println(cursor.contents);
cursor = cursor.next;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.