Node addTwoLists(Node first, Node second)
while (first != null || second != null)
sum = carry + (first != null ? first.data : 0)
+ (second != null ? second.data : 0);
carry = (sum >= 10) ? 1 : 0;
temp.next = new Node(carry);
void printList(Node head)
Console.Write(head.data + " ");
public static void Main(String[] args)
LinkedList list = new LinkedList();
list.head1 = new Node(7);
list.head1.next = new Node(5);
list.head1.next.next = new Node(9);
list.head1.next.next.next = new Node(4);
list.head1.next.next.next.next = new Node(6);
Console.Write("First List is ");
list.printList(list.head1);
list.head2 = new Node(8);
list.head2.next = new Node(4);
Console.Write("Second List is ");
list.printList(list.head2);
Node rs = list.addTwoLists(list.head1, list.head2);
Console.Write("Resultant List is ");