Question about reference variable


  • -4
    J

    The follows is my solution:
    /**

    • Definition for singly-linked list.
    • public class ListNode {
    • int val;
      
    • ListNode next;
      
    • ListNode(int x) {
      
    •     val = x;
      
    •     next = null;
      
    • }
      
    • }
      */
      public class Solution {
      public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
      ListNode a = l1;
      ListNode b = l2;
      ListNode c = null;
      ListNode start = c;
      while(a!=null && b!=null){
      if(a.val<=b.val){
      c = new ListNode(a.val);
      a = a.next;
      }
      else if(a.val>b.val){
      c = new ListNode(b.val);
      b = b.next;
      }
      c = c.next;
      }
      if(a!=null && b==null){
      c = a;
      }
      else if(a==null && b!= null){
      c = b;
      }
      return start;
      }
      }

    well when the input are: {}, {0}
    the output is:{}
    well it is expected to be: {0}
    I think it must be the error about "start" but do not know why


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.