content: package org.redquark.tutorials.leetcode;
  
  /**
   * @author Anirudh
   */
  public class AddTwoNumbers {
  
      /**
       * @param l1 - head of the first linked list
       * @param l2 - head of the second linked list
       * @return - head of the list with the sum
       */
      private static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
          // Head of the new linked list - this is the head of the resultant list
          ListNode head = null;
          // Reference of head which is null at this point
          ListNode temp = null;
          // Carry
          int carry = 0;
          // Loop for the two lists
          while (l1 != null || l2 != null) {
              // At the start of each iteration, we should add carry from the last iteration
              int sum = carry;
              // Since the lengths of the lists may be unequal, we are checking if the
              // current node is null for one of the lists
              if (l1 != null) {
                  sum += l1.val;
                  l1 = l1.next;
              }
              if (l2 != null) {
                  sum += l2.val;
                  l2 = l2.next;
              }
              // At this point, we will add the total sum % 10 to the new node
              // in the resultant list
              ListNode node = new ListNode(sum % 10);
              // Carry to be added in the next iteration
              carry = sum / 10;
              // If this is the first node or head
              if (temp == null) {
                  temp = head = node;
              }
              // For any other node
              else {
                  temp.next = node;
                  temp = temp.next;
              }
          }
          // After the last iteration, we will check if there is carry left
          // If it's left then we will create a new node and add it
          if (carry > 0) {
              temp.next = new ListNode(carry);
          }
          return head;
      }
  
  
      /**
       * This class represents each node in the linked list
       */
      static class ListNode {
          final int val;
          ListNode next;
  
          ListNode(int val) {
              this.val = val;
          }
      }
  
      public static void main(String[] args) {
          ListNode head1 = new ListNode(2);
          head1.next = new ListNode(4);
          head1.next.next = new ListNode(3);
  
          ListNode head2 = new ListNode(5);
          head2.next = new ListNode(6);
          head2.next.next = new ListNode(4);
  
          ListNode result = addTwoNumbers(head1, head2);
          while (result != null) {
              System.out.print(result.val + " ");
              result = result.next;
          }
      }
  }
  