Microsoft Interview Question

Given an integer write a function that converts the input into a linkedList where each node corresponds to a number of the integer. Eg: 25697 == 2 -> 5 -> 6 -> 9 -> 7 Then write a function that takes 2 linkedList, add the corresponding integers and return a third list with the result.

Interview Answers

Anonymous

Feb 18, 2013

The is a blog discussing how to add two numbers in two lists, with the following link: http://codercareer.blogspot.com/2013/02/no-40-add-on-lists.html

1

Anonymous

Feb 18, 2013

Hi Harry, I used your website a lot when I was studying for interviews. Certainly helped me get the internship. Thanks for your time!

Anonymous

Feb 21, 2013

/* Jun Zheng, Rice Univ C++, Xcode 4.5.2 Interview question of GS Convert a number to linked list. Need reverse. */ node* numToLinkedList(int num){ if(abs(num)* p=new node(); p->next=numToLinkedList(num/10); p->data=num%10; return p; } //Reverse the list void reverse(node* &phead){ if(phead==NULL ||length(phead)==1) return; node* p1=phead->next; node* p2=NULL; phead->next=NULL; while(p1->next != NULL){ p2=p1->next; p1->next=phead; phead=p1; p1=p2; } p1->next=phead; phead=p1; }

Anonymous

May 26, 2020

//Converting an Integer into Linked List DS public ListNode ConvertIntegerIntoList(int num) { int r=0; ListNode dummy=new ListNode(-1); ListNode temphead=dummy; while(num!=0) { r=num%10; ListNode n=new ListNode(r); dummy.next=n; dummy=dummy.next; num=num/10; } return reverse(temphead.next); } public ListNode reverse(ListNode head) { ListNode current=head; ListNode previous=null; ListNode next=null; while(current!=null) { next=current.next; current.next=previous; previous=current; current=next; } return previous; }