代码在一个环境中有效,但在另一个环境中无效
Codes work in one environment but not another
该函数在LeetCode的执行环境下运行正确,输出正确[1,1,2,3,4,4]
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyNode = new ListNode(-1);
ListNode l3 = dummyNode;
while(l1!=null &&l2!=null){
if(l1.val<l2.val){
l3.next=l1;
l1=l1.next;
}
else{
l3.next=l2;
l2=l2.next;
}
l3=l3.next;
}
l3.next=l1==null?l2:l1;
return dummyNode.next;
}
我扩展以添加其他代码并在 Eclipse 中调用了此函数,但它们不起作用。问题在
l3.next=l1;
和
l3.next=l2;
源代码:
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class MyLeetCode {
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyNode = new ListNode(-1);
ListNode l3 = dummyNode;
while(l1!=null &&l2!=null){
if(l1.val<l2.val){
l3.next=l1;
l1=l1.next;
}
else{
l3.next=l2;
l2=l2.next;
}
l3=l3.next;
}
l3.next=l1==null?l2:l1;
return dummyNode.next;
}
public static void main(String[] args) {
int[] A = {1,2,4};
int[] B = {1,3,4};
ListNode currA = null, currB=null;
for(int i=A.length-1; i>=0; i--) {
ListNode ls =new ListNode(A[i], currA);
currA=ls;
}
for(int i=A.length-1; i>=0; i--) {
ListNode ls=new ListNode(B[i], currB);
currB=ls;
}
ListNode ret = mergeTwoLists(currA, currB);
while(ret!=null)
System.out.print(ret.val + " ");
}
}
问题
问题在
while(ret!=null)
System.out.print(ret.val + " ");
ret
需要前进
- 目前它将运行无限(只打印第一个节点值)
更正
while (ret! = null) {
System.out.print(ret.val + " ");
ret = ret.next;
}
全苏宁
public class MyLeetCode {
// individual node in the List
static class ListNode {
private final int val;
private ListNode next;
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
protected static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(-1, null); // to reduce custom check for head
ListNode current = dummyHead;
// merge while both the lists have value
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
// concatenate the list with remaining elements(if any)
current.next = l1 == null ? l2 : l1;
// return begin of the merged list
return dummyHead.next;
}
// create list from the input array. preserves the order of nums
protected static ListNode createList(int[] nums) {
ListNode head = null;
for (int i = nums.length - 1; i >= 0; i--) {
ListNode ls = new ListNode(nums[i], head);
head = ls;
}
return head;
}
// prints the list - starting from input node till end of list
protected static void printList(ListNode node) {
while (node != null) {
System.out.print(node.val + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
ListNode headA = createList(new int[] {1, 2, 4});
ListNode headB = createList(new int[] {1, 3, 4, 5});
printList(headA);
printList(headB);
ListNode mergedHead = mergeTwoLists(headA, headB);
printList(mergedHead);
}
}
该函数在LeetCode的执行环境下运行正确,输出正确[1,1,2,3,4,4]
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyNode = new ListNode(-1);
ListNode l3 = dummyNode;
while(l1!=null &&l2!=null){
if(l1.val<l2.val){
l3.next=l1;
l1=l1.next;
}
else{
l3.next=l2;
l2=l2.next;
}
l3=l3.next;
}
l3.next=l1==null?l2:l1;
return dummyNode.next;
}
我扩展以添加其他代码并在 Eclipse 中调用了此函数,但它们不起作用。问题在
l3.next=l1;
和
l3.next=l2;
源代码:
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class MyLeetCode {
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyNode = new ListNode(-1);
ListNode l3 = dummyNode;
while(l1!=null &&l2!=null){
if(l1.val<l2.val){
l3.next=l1;
l1=l1.next;
}
else{
l3.next=l2;
l2=l2.next;
}
l3=l3.next;
}
l3.next=l1==null?l2:l1;
return dummyNode.next;
}
public static void main(String[] args) {
int[] A = {1,2,4};
int[] B = {1,3,4};
ListNode currA = null, currB=null;
for(int i=A.length-1; i>=0; i--) {
ListNode ls =new ListNode(A[i], currA);
currA=ls;
}
for(int i=A.length-1; i>=0; i--) {
ListNode ls=new ListNode(B[i], currB);
currB=ls;
}
ListNode ret = mergeTwoLists(currA, currB);
while(ret!=null)
System.out.print(ret.val + " ");
}
}
问题
问题在
while(ret!=null)
System.out.print(ret.val + " ");
ret
需要前进- 目前它将运行无限(只打印第一个节点值)
更正
while (ret! = null) {
System.out.print(ret.val + " ");
ret = ret.next;
}
全苏宁
public class MyLeetCode {
// individual node in the List
static class ListNode {
private final int val;
private ListNode next;
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
protected static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(-1, null); // to reduce custom check for head
ListNode current = dummyHead;
// merge while both the lists have value
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
// concatenate the list with remaining elements(if any)
current.next = l1 == null ? l2 : l1;
// return begin of the merged list
return dummyHead.next;
}
// create list from the input array. preserves the order of nums
protected static ListNode createList(int[] nums) {
ListNode head = null;
for (int i = nums.length - 1; i >= 0; i--) {
ListNode ls = new ListNode(nums[i], head);
head = ls;
}
return head;
}
// prints the list - starting from input node till end of list
protected static void printList(ListNode node) {
while (node != null) {
System.out.print(node.val + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
ListNode headA = createList(new int[] {1, 2, 4});
ListNode headB = createList(new int[] {1, 3, 4, 5});
printList(headA);
printList(headB);
ListNode mergedHead = mergeTwoLists(headA, headB);
printList(mergedHead);
}
}