我試圖將兩個排序串列合并為一個并使用顯示方法列印該串列。但我得到了,我得到了這個輸出:
-1 --> -21 --> 0 --> 2 --> 3 --> 5 --> 44 --> null
代替:
-21 --> 0 --> 2 --> 3 --> 5 --> 44 --> null
為什么我以前會得到這個額外的-1?我知道我已經在第 15 行 ListNode dummy= new ListNode(-1);初始化了-1. 和-1我得到的一樣嗎?我該如何跳過這個?
這是我的代碼:
public class Mergelists {
private static ListNode head;
private static class ListNode{
int val;
ListNode next;
public ListNode(int val){
this.val= val;
this.next= null;
}
}
public static ListNode merge(ListNode l1, ListNode l2){
ListNode dummy = new ListNode(-1);
head = dummy;
while (l1 != null && l2 != null){
if (l1.val<l2.val){
dummy.next=l1;
l1=l1.next;
}
else
{
dummy.next=l2;
l2=l2.next;
}
dummy=dummy.next;
}
if (l1 !=null)
dummy.next=l1;
if (l2 !=null)
dummy.next=l2;
return head.next;
}
public static void display() {
ListNode current = head;
while(current != null) {
System.out.print(current.val " --> ");
current = current.next;
}
System.out.print("null");
}
public static void main(String[] args) {
ListNode head1 = new ListNode(-21);
head1.next = new ListNode(3);
head1.next.next = new ListNode(5);
// -21->3->5 LinkedList created
ListNode head2 = new ListNode(0);
head2.next = new ListNode(2);
head2.next.next = new ListNode(44);
// 0->2->44 LinkedList created
merge(head1, head2);
display();
}
uj5u.com熱心網友回復:
當方法 merge 回傳時,head串列的 仍然指向虛擬節點。
您需要在 return 陳述句之前添加這一行:
head = head.next;
但這并不是最嚴重的錯誤——head屬性以及所有這些方法都不應該是static.
修飾符static意味著頭節點正在串列的所有實體之間共享,這是不正確的。
這將是實作單鏈表的正確方法。我還建議您實作諸如add()和之類的方法remove()。
class MyList {
private ListNode head;
private static class ListNode {
private int val;
private ListNode next;
public ListNode(int val) {
this.val = val;
this.next = null;
}
}
public ListNode merge(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
head = dummy;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
dummy.next = l1;
l1 = l1.next;
} else {
dummy.next = l2;
l2 = l2.next;
}
dummy = dummy.next;
}
if (l1 != null)
dummy.next = l1;
if (l2 != null)
dummy.next = l2;
head = head.next;
return head.next;
}
public void display() {
ListNode current = head;
while (current != null) {
System.out.print(current.val " --> ");
current = current.next;
}
System.out.print("null");
}
public static void main(String[] args) {
MyList mergedList = new MyList();
ListNode head1 = new ListNode(-21);
head1.next = new ListNode(3);
head1.next.next = new ListNode(5);
// -21->3->5 LinkedList created
ListNode head2 = new ListNode(0);
head2.next = new ListNode(2);
head2.next.next = new ListNode(44);
// 0->2->44 LinkedList created
mergedList.merge(head1, head2);
mergedList.display();
}
}
輸出:
-21 --> 0 --> 2 --> 3 --> 5 --> 44 --> null
uj5u.com熱心網友回復:
該display方法從類成員變數開始列印節點head。在merge方法中,head由 -1 初始化,這就是它總是從 -1 列印的原因。一個簡單的方法是在display方法中添加一個引數。
public static void display(ListNode head) {
ListNode current = head;
while(current != null) {
System.out.print(current.val " --> ");
current = current.next;
}
System.out.print("null");
}
然后將merge方法的結果存盤在一個變數中,并display從方法中呼叫main方法。
ListNode res= merge(head1, head2);
display(res);
uj5u.com熱心網友回復:
你的頭指向-1,它是虛擬節點。將你的頭指向下一個節點。
public static ListNode merge(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
head = dummy;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
dummy.next = l1;
l1 = l1.next;
} else {
dummy.next = l2;
l2 = l2.next;
}
dummy = dummy.next;
}
if (l1 != null)
dummy.next = l1;
if (l2 != null)
dummy.next = l2;
##moving you head to next node.
head = head.next;
return head.next;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/482717.html
