nightingale
BAN USERIf the list contains duplicate data, this logic fails
- nightingale May 17, 2013Assuming the first list is list1,
second list is list2, result has to be stored in list3
if ((list1==null) and (list2==null))
{
list3==null;
}
If (list1 == null)
//traverse through list 2 and assign list3 = list2
if(list2 == null)
//traverse through list 1 and assign list3=list1
//assuming all error cases are handled
consider this function
int carry =0 ;
add(struct node *list1, struct node *list2, struct node **list3, int *carry)
{
carry = 0;
if((list1->next) && (list2->next))
{
add(list1->next, list2->next, &list3->next, &carry);
}
*list3->data = list1->data + list2->data + *carry;
if(*list3->data > 10)
{
*list3->data = *list3->data % 10;
*carry = 1;
}
}
if carry exists then one more linked list node has to be created at head node to accomodate carry.
k farthest points from origin form a circle of radius k. Hence the values |x| and |y| can be in range from 0 to k, with special cases when |x| = k, then y=0, and viceversa.
- nightingale May 17, 2013Hence looping through all values , we can consider only those pairs which have |x|<= k, |y|<=k and |y|^2 = |k|^2-|x|^2.
this takes time O(n)