ÀýÈ磺9>9>9>NULL + 1>NULL =>
1>0>0>0>NULL
¿Ï¶¨ÊÇʹÓõݹéÀ²£¬²»È»Ã»°ì·¨½â¾ö½øÎ»+1ÎÊÌ⣬ÒòΪÕâʱºòÒªÈÃÇ°ÃæµÄ½Úµã¼Ó1£¬¶øÎÒÃǵĵ¥Á´±íÊÇÓÀÔ¶Ö¸ÏòǰµÄ¡£
´ËÍâ¶ÔÓÚ999+1=1000£¬Ðµõ½µÄÖµµÄλÊý£¨4룩±ÈÔÀ´µÄÁ½¸öÖµ£¨1¸ö1룬1¸ö3룩¶¼¶à£¬ËùÒÔÎÒÃǽ«±íÍ·µÄÖµÉèÖÃΪ0£¬Èç¹û¶à³öһλÀ´£¬¾ÍÔÝʱ´æ·Åµ½±íÍ·¡£µÝ¹é½áÊøºó£¬Èç¹û±íͷΪ1£¬¾ÍÔÚеÄÁ´±íÍâÔÙ¼ÓÒ»¸öеıíÍ·¡£
//head1 length > head2, so M > N
public static int Add(Link head1, Link head2, ref Link newHead, int M, int N)
{
// goto the end
if (head1 == null)
return 0;
int temp = 0;
int result = 0;
newHead = new Link(null, 0);
if (M > N)
{
result = Add(head1.Next, head2, ref newHead.Next, M ¨C 1, N);
temp = head1.Data + result;
newHead.Data = temp % 10;
return temp >= 10
1 : 0;
}
else // M == N
{
result = Add(head1.Next, head2.Next, ref newHead.Next, M ¨C 1, N ¨C 1);
temp = head1.Data + head2.Data + +result;
newHead.Data = temp % 10;
return temp >= 10
1 : 0;
}
}
ÕâÀï¼ÙÉèhead1±Èhead2³¤£¬¶øÇÒM¡¢N·Ö±ðÊÇhead1ºÍhead2µÄ³¤¶È¡£