struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){ struct ListNode* dummy = malloc(sizeof(struct ListNode)); struct ListNode* cur = dummy; int t = 0; while(l1 || l2 || t){ if(l1) t += l1->val,l1=l1->next; if(l2) t += l2->val,l2=l2->next; cur->next = malloc(sizeof(struct ListNode)); cur->next->val = t%10; cur->next->next = NULL; cur = cur->next; t /= 10; } return dummy->next; }解释一下上面的代码
时间: 2024-04-20 22:22:38 浏览: 220
这段代码是一个用于两个链表表示的数相加的函数。函数接受两个输入参数l1和l2,这两个参数分别表示两个链表的头指针。
首先,代码中创建了一个名为dummy的虚拟节点,用于保存结果链表的头指针。然后创建一个名为cur的指针,用于遍历结果链表。
接下来,使用一个循环来遍历l1和l2链表,同时处理进位。循环条件是l1或l2不为空,或者有进位t不为0。在每次循环中,通过判断l1和l2是否为空,来确定当前位相加的值。
如果l1不为空,将l1的值加到t上,并将l1指针移到下一个节点。如果l2不为空,将l2的值加到t上,并将l2指针移到下一个节点。
然后,创建一个新的节点,并将t的个位数赋值给新节点的val。将新节点连接到结果链表的末尾,并将cur指针移到新节点。
最后,更新进位t为t的十位数。重复上述步骤直到l1和l2都为空,且没有进位。
最后,返回结果链表的头指针dummy->next。代码中使用了动态内存分配函数malloc来创建新节点和虚拟节点,在使用完毕后需要注意释放这些内存空间,以免造成内存泄漏。
相关问题
用C语言改写,将打印输出的结果中重复的数去掉#include <stdio.h>#include <stdlib.h>struct ListNode { int val; struct ListNode *next;};struct ListNode* mergeList(struct ListNode* l1, struct ListNode* l2){ struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode)); // 新建一个虚拟头节点 dummy->val = 0; dummy->next = NULL; struct ListNode* cur = dummy; while (l1 && l2) { if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; } else { cur->next = l2; l2 = l2->next; } cur = cur->next; } if (l1) cur->next = l1; // 将剩余的节点加入到新链表中 if (l2) cur->next = l2; return dummy->next;}int main() { int n, m, num; scanf("%d%d", &n, &m); struct ListNode* l1 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* l2 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* p1 = l1; struct ListNode* p2 = l2; for (int i = 0; i < n; i++) { scanf("%d", &num); p1->next = (struct ListNode*)malloc(sizeof(struct ListNode)); p1->next->val = num; p1->next->next = NULL; p1 = p1->next; } for (int i = 0; i < m; i++) { scanf("%d", &num); p2->next = (struct ListNode*)malloc(sizeof(struct ListNode)); p2->next->val = num; p2->next->next = NULL; p2 = p2->next; } struct ListNode* res = mergeList(l1->next, l2->next); while (res) { printf("%d ", res->val); res = res->next; } return 0;}
以下是用 C 语言改写,并去掉重复数的代码:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* mergeList(struct ListNode* l1, struct ListNode* l2){
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode)); // 新建一个虚拟头节点
dummy->val = 0;
dummy->next = NULL;
struct ListNode* cur = dummy;
while (l1 && l2) {
if (l1->val <= l2->val) {
cur->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
if (l1) cur->next = l1; // 将剩余的节点加入到新链表中
if (l2) cur->next = l2;
return dummy->next;
}
int main() {
int n, m, num;
scanf("%d%d", &n, &m);
struct ListNode* l1 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* l2 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* p1 = l1;
struct ListNode* p2 = l2;
for (int i = 0; i < n; i++) {
scanf("%d", &num);
p1->next = (struct ListNode*)malloc(sizeof(struct ListNode));
p1->next->val = num;
p1->next->next = NULL;
p1 = p1->next;
}
for (int i = 0; i < m; i++) {
scanf("%d", &num);
p2->next = (struct ListNode*)malloc(sizeof(struct ListNode));
p2->next->val = num;
p2->next->next = NULL;
p2 = p2->next;
}
struct ListNode* res = mergeList(l1->next, l2->next);
// 去除重复数
int hash[10001] = {0}; // 哈希表,假设节点值范围在 [0, 10000]
struct ListNode* cur = res;
struct ListNode* pre = NULL;
while (cur) {
if (hash[cur->val]) { // 如果当前节点值已经出现过,则删除当前节点
pre->next = cur->next;
free(cur);
cur = pre->next;
} else { // 如果当前节点值没有出现过,则将其加入哈希表,并继续遍历下一个节点
hash[cur->val] = 1;
pre = cur;
cur = cur->next;
}
}
// 输出结果
cur = res;
while (cur) {
printf("%d ", cur->val);
cur = cur->next;
}
// 释放内存
cur = res;
while (cur) {
struct ListNode* temp = cur;
cur = cur->next;
free(temp);
}
return 0;
}
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *readlist(); struct ListNode *deletem( struct ListNode *L, int m ); void printlist( struct ListNode *L ) { struct ListNode *p = L; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { int m; struct ListNode *L = readlist(); scanf("%d", &m); L = deletem(L, m); printlist(L); return 0; } /* 你的代码将被嵌在这里 */
以下是完整的代码实现:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
// 读取链表
struct ListNode *readlist() {
int n;
scanf("%d", &n);
struct ListNode *head = NULL, *tail = NULL;
while (n--) {
struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));
scanf("%d", &(node->data));
node->next = NULL;
if (!head) {
head = tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
// 删除值为 m 的节点
struct ListNode *deletem(struct ListNode *L, int m) {
struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode));
dummy->next = L;
struct ListNode *prev = dummy, *curr = L;
while (curr) {
if (curr->data == m) {
prev->next = curr->next;
free(curr);
curr = prev->next;
} else {
prev = curr;
curr = curr->next;
}
}
L = dummy->next;
free(dummy);
return L;
}
// 打印链表
void printlist(struct ListNode *L) {
struct ListNode *p = L;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main() {
int m;
struct ListNode *L = readlist();
scanf("%d", &m);
L = deletem(L, m);
printlist(L);
return 0;
}
函数 `readlist()` 用于读取链表,首先读入链表节点数 `n`,然后循环读入每个节点的值,将其插入链表尾部。函数 `deletem()` 用于删除链表中值为 `m` 的节点,采用双指针法,遍历链表,若当前节点值为 `m`,则删除当前节点,否则继续遍历,直到链表末尾。需要注意的是,为了方便删除链表头节点,可以设置一个哑节点 `dummy`,它的 `next` 指向链表头节点,这样就可以通过 `prev` 指针来删除当前节点。最后,函数 `printlist()` 用于打印链表。
阅读全文