1.题目要求:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeNodes(struct ListNode* head){
struct ListNode* cur = head;
int count = 0;
//1.遍历结点,求出结点数
while(cur){
count++;
cur = cur->next;
}
cur = head;
//2.创造数组,把结点值依次放入数组中
int* number = (int*)malloc(sizeof(int) * count);
int j = 0;
while(cur){
number[j] = cur->val;
j++;
cur = cur->next;
}
//3.再创一个数组,用于解决0之间和的存储问题
int* sum_number = (int*)malloc(sizeof(int) * count);
int f = 0;
int i = 0;
int sum = 0;
//4.开始遍历数组,把0之间的和放入到sum_number数组中
for(i = 1;i < j - 1;i++){
if(number[i + 1] != 0){
sum += number[i];
}else{
sum += number[i];
sum_number[f] = sum;
f++;
sum = 0;
i++;
}
}
cur = head;
i = 0;
//5.把sum_number中的值赋给链表
while(cur){
cur->val = sum_number[i];
i++;
if(i == f){
break;
}
cur = cur->next;
}
//6.最后把剩余链表置空,返回头结点
cur->next = NULL;
return head;
}
标签:结点,cur,int,sum,number,++,2181,leetcode,struct
From: https://blog.csdn.net/m0_54244065/article/details/140855607