Question
I'm receiving an error on my assignment! this is the error: java.lang.AssertionError: expected: but was: Suppose you are given two linked lists of integers, the
I'm receiving an error on my assignment!
this is the error: java.lang.AssertionError: expected: but was:
Suppose you are given two linked lists of integers, the values in which are non-decreasing (e.g., 1 -> 3 -> 5 and 2 -> 4 -> 6).
Try to implement a method to merge these two lists by splicing together their nodes so that we can get a sorted list that contains all elements from them (e.g., 1 -> 2 -> 3 -> 4 -> 5 -> 6).
- Inputs: the header nodes of two sorted linked lists;
- Outputs: the header node of the merged linked list.
Note: The ListNode class is provided in the template file. Please keep it when you submit the code. Also, please don't modify other existing class/method headers in the template file.
Hints: Consider edge cases (e.g., null inputs).
This is the coding I did.. No error in the Java environment but the school system gave me the error.
public class Solution { 1 2 * The definition for the linked list. DO NOT modify this class. */ public static class ListNode { int value; ListNode next; ListNode(int value) { this.value = value; } } /*** * The method for you to implement. * DO NOT change the method header. * public ListNode merge (ListNode 11, ListNode 12) { // TODO ListNode head = null; // the header node of the merged linked list. if (11 != null && 12 != null) { // verify if both the linked lists are not empty. // select the first element of the node // compare the first elements of two linked lists if (11.value = 12.value) { // if the li's first value is smaller or equal compare to l2's first value head = new ListNode(11.value); // set ll's first node as head of the merged list 11 = 11.next; // point to the next of list 11. } else { // if the 12's first value is smaller compare to li's first value head = new ListNode ( 12.value); // set 12's first node as head of the merged list 12 = 12.next; // point to the next of list 12. 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 ListNode tail = head; // save the head as tail where further values will be merged. // read both the linked lists till anyone/both of the list(s) become empty. while (11 != null && 12 != null) { if (11.valueStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started