两数相加leetcode

第一个是测试用例代码,测试的是两个带头的逆序链表相加,并且有反转操作

但是题目要求的是不带头链表直接相加,不需要逆转,输出结果也是逆序的,

题解放在第二个代码中

复制代码
#include<stdio.h>
#include<stdlib.h>
typedef struct ListNode {
	int val;
	struct ListNode* next;
}List;

List* reverse(List* L) {
	List* p = L;
	List* q = NULL;
	while (p != NULL) {
		List* temp = p->next;
		p->next = q;
		q = p;
		p = temp;
	}

	List* head = (List*)(malloc)(sizeof(List));
	head->next = q;
	List* temp = head;
	while (temp->next->next != NULL) {
		temp = temp->next;
	}
	temp->next = NULL;
	return head;
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
	List*L1=reverse(l1);
	L1->val = 0;
	List*L2=reverse(l2);
	L2->val = 0;
	List* L3 = (List*)(malloc)(sizeof(List));
	L3->next = NULL;
	L3->val = 0;
	int flag = 0;
	while (L1 != NULL || L2 != NULL) {
		List* temp = (List*)(malloc)(sizeof(List));
		temp->next = NULL;
		temp->val = 0;
		if (L1) {
			temp->val += L1->val;
			L1 = L1->next;
		}
		if (L2) {
			temp->val += L2->val;
			L2 = L2->next;
		}
		if (flag == 1) {
			temp->val += 1;
			flag = 0;
		}//进位
		if (temp->val >= 10) {
			temp->val= temp->val % 10;
			flag = 1;
		}//大于10进位
		List* cur = L3;
		while (cur->next != NULL) {
			cur = cur->next;
		}
		cur->next = temp;

		if (L1 == NULL && L2 == NULL && flag == 1) {
			List* digital = (List*)(malloc)(sizeof(List));
			digital->val = 1;
			digital->next = NULL;
			temp->next = digital;
		}
	}
	L3->next->val = 0;
	return reverse(L3->next);
}
List* makeList() {
	List* L = (List*)(malloc)(sizeof(List));
	L->next = NULL;
	int num;
	while (1) {
		scanf("%d", &num);
		if (num == 0) {
			break;
		}
		List* temp = (List*)(malloc)(sizeof(List));
		temp->next = NULL;
		temp->val = num;
		List* p = L;
		while (p->next != NULL) {
			p = p->next;
		}
		p->next = temp;
	}
	return L;
}
void print_List(List* L) {
	L = L->next; // 跳过头节点
	while (L != NULL) {
		printf("%d ", L->val);
		L = L->next;
	}
	printf("\n");
}

int main() {
	List* L1 = makeList();
	printf("打印L1\n");
	print_List(L1);
	List* L3=reverse(L1);
	List* L2= makeList();
	printf("打印L2\n");
	print_List(L2);
	List* L4 = reverse(L2);

	printf("打印L3\n");
	print_List(L3);
	printf("打印L4\n");
	print_List(L4);

	List* L5 = addTwoNumbers(L3, L4);
	printf("打印L5\n");
	print_List(L5);
}


struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {

	struct ListNode* L3 = (struct ListNode*)(malloc)(sizeof(struct ListNode));
	L3->next = NULL;
    L3->val=0;
	int flag = 0;
	while (l1 != NULL || l2 != NULL) {
		struct ListNode* temp = (struct ListNode*)(malloc)(sizeof(struct ListNode));
		temp->next = NULL;
		temp->val = 0;
		if (l1) {
			temp->val += l1->val;
			l1 = l1->next;
		}
		if (l2) {
			temp->val += l2->val;
			l2 = l2->next;
		}
		if (flag == 1) {
			temp->val += 1;
			flag = 0;
		}//进位
		if (temp->val >= 10) {
			temp->val= temp->val % 10;
			flag = 1;
		}//大于10进位
		struct ListNode* cur = L3;
		while (cur->next != NULL) {
			cur = cur->next;
		}
		cur->next = temp;

		if (l1 == NULL && l2 == NULL && flag == 1) {
			struct ListNode* digital = (struct ListNode*)(malloc)(sizeof(struct ListNode));
			digital->val = 1;
			digital->next = NULL;
			temp->next = digital;
		}
	}
	return (L3->next);
}
相关推荐
fie88895 小时前
NSCT(非下采样轮廓波变换)的分解和重建程序
算法
晨晖26 小时前
单链表逆转,c语言
c语言·数据结构·算法
沐雪架构师6 小时前
大模型Agent面试精选15题(第四辑)-Agent与RAG(检索增强生成)结合的高频面试题
面试·职场和发展
YoungHong19926 小时前
面试经典150题[072]:从前序与中序遍历序列构造二叉树(LeetCode 105)
leetcode·面试·职场和发展
im_AMBER7 小时前
Leetcode 78 识别数组中的最大异常值 | 镜像对之间最小绝对距离
笔记·学习·算法·leetcode
鼾声鼾语8 小时前
matlab的ros2发布的消息,局域网内其他设备收不到情况吗?但是matlab可以订阅其他局域网的ros2发布的消息(问题总结)
开发语言·人工智能·深度学习·算法·matlab·isaaclab
LYFlied8 小时前
【每日算法】LeetCode 25. K 个一组翻转链表
算法·leetcode·链表
Swizard8 小时前
别再迷信“准确率”了!一文读懂 AI 图像分割的黄金标尺 —— Dice 系数
python·算法·训练
s09071368 小时前
紧凑型3D成像声纳实现路径
算法·3d·声呐·前视多波束
可爱的小小小狼8 小时前
算法:二叉树遍历
算法