例如l1->val,以及后面的tail->next,为什么val和next没有相关定义却可以使用,本身代表什么含义?
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *head = NULL, *tail = NULL;
int carry = 0;
while (l1 || l2) {
int n1 = l1 ? l1->val : 0;
int n2 = l2 ? l2->val : 0;
int sum = n1 + n2 + carry;
if (!head) {
head = tail = malloc(sizeof(struct ListNode));
tail->val = sum % 10;
tail->next = NULL;
} else {
tail->next = malloc(sizeof(struct ListNode));
tail->next->val = sum % 10;
tail = tail->next;
tail->next = NULL;
}
carry = sum / 10;
if (l1) {
l1 = l1->next;
}
if (l2) {
l2 = l2->next;
}
}
if (carry > 0) {
tail->next = malloc(sizeof(struct ListNode));
tail->next->val = carry;
tail->next->next = NULL;
}
return head;
}
class Solution {
public int getMaximumGenerated(int n) {
if(n < 2) {
return n;
}
int max = 1;
int[] nums = new int[n + 1];
// nums[0] = 0;
nums[1] = 1;
for (int i = 2; i <= n; i++) {
nums[i] = nums[i / 2] +i % 2 * nums[i / 2 + 1]; // 奇数判断为0
/*if(i % 2 == 0) { // 偶数->[i/2]
nums[i] = nums[i / 2];
}else { // 奇数->[i/2] + [i/2+1]
nums[i] = nums[i / 2] + nums[i / 2 + 1];
}*/
max = Math.max(max, nums[i]);
}
return max;
}
}
你说笑话吧,怎么可能没有定义就能使用。你看一下struct ListNode结构体的定义啊。