我正在尝试将两个链接列表连接在一起,其中第二个列表将附加在第一个列表的尾部之后。在我的追加方法中,我想要获取要连接在一起的两个列表,然后将最后一个列表连接到末尾。我无法将当前位置分配给第二个列表的头部。对我下一步做什么有什么建议吗?
public class Link {
public long dData; // data item
public Link next; // next link in list
// -------------------------------------------------------------
public Link(long d) // constructor
{
dData = d;
}
// -------------------------------------------------------------
public void displayLink() // display this link
{
System.out.print(dData + " ");
}
// -------------------------------------------------------------
} // end class Link
public class FirstLastList {
private Link first; // ref to first link
private Link last; // ref to last link
// -------------------------------------------------------------
public FirstLastList() // constructor
{
first = null; // no links on list yet
last = null;
}
// -------------------------------------------------------------
public boolean isEmpty() // true if no links
{
return first == null;
}
// -------------------------------------------------------------
public void insertFirst(long dd) // insert at front of list
{
Link newLink = new Link(dd); // make new link
if (isEmpty()) // if empty list,
{
last = newLink; // newLink <-- last
}
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
// -------------------------------------------------------------
public void insertLast(long dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if (isEmpty()) // if empty list,
{
first = newLink; // first --> newLink
} else {
last.next = newLink; // old last --> newLink
}
last = newLink; // newLink <-- last
}
// -------------------------------------------------------------
public long deleteFirst() // delete first link
{ // (assumes non-empty list)
long temp = first.dData;
if (first.next == null) // if only one item
{
last = null; // null <-- last
}
first = first.next; // first --> old next
return temp;
}
// -------------------------------------------------------------
public void displayList() {
System.out.print("List (first-->last): ");
Link current = first; // start at beginning
while (current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
// -------------------------------------------------------------
public void append(FirstLastList list1, FirstLastList list2) {
Link current = first;
while (list1 != null) {
current = current.next;
}
current.next = list2.first;
}
} // end class FirstLastList
public class FirstLastApp {
public static void main(String[] args) { //make a new list
FirstLastList theList = new FirstLastList();
theList.insertFirst(22); // insert at front
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertLast(11); // insert at rear
theList.insertLast(33);
theList.insertLast(55);
theList.displayList(); // display the list
theList.deleteFirst(); // delete first two items
theList.deleteFirst();
theList.displayList(); // display again
FirstLastList theList2 = new FirstLastList();
theList.insertFirst(22); // insert at front
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertLast(11); // insert at rear
theList.insertLast(33);
theList.insertLast(55);
theList.displayList(); // display the list
theList.deleteFirst(); // delete first two items
theList.deleteFirst();
theList.displayList(); // display again
append(theList, theList2);
theList.displayList(); // display again
} // end main()
} // end class FirstLastApp
最佳答案
在append()方法中:
Link current = first;
while(current.next != null) {
current = current.next;
}
current.next = list2.first;
当当前节点到达最后一个节点时,其.next
将为空。那就是您加入第二个列表的时候。
关于java - 如何将一个链表附加到另一个链表的末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54856301/