1019. Next Greater Node In Linked List
We are given a linked list with head as the first node. Let's number the nodes in the list: node_1, node_2, node_3, ... etc.
Each node may have a next larger value: for node_i, next_larger(node_i) is the node_j.val such that j > i, node_j.val > node_i.val, and j is the smallest possible choice. If such a j does not exist, the next larger value is 0.
Return an array of integers answer, where answer[i] = next_larger(node_{i+1}).
Note that in the example inputs (not outputs) below, arrays such as [2,1,5] represent the serialization of a linked list with a head node value of 2, second node value of 1, and third node value of 5.
Example 1:
Input: [2,1,5]
Output: [5,5,0]
Example 2:
Input: [2,7,4,3,5]
Output: [7,0,5,5,0]
Example 3:
Input: [1,7,5,1,9,2,5,1]
Output: [7,9,9,9,0,5,0,0]
Note:
- 1 <= node.val <= 10^9 for each node in the linked list.
- The given list has length in the range [0, 10000].
解题思路:
法一:
// 执行用时 : 1448 ms, 在Next Greater Node In Linked List的C提交中击败了40.63% 的用户
// 内存消耗 : 33.2 MB, 在Next Greater Node In Linked List的C提交中击败了100.00% 的用户
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* nextLargerNodes(struct ListNode* head, int* returnSize){
//特殊情况
if(head == NULL){
return NULL;
}
struct ListNode* p_after = head;//用于遍历当前结点之后的结点,找到下一个更大结点
struct ListNode* curNode = head;//指向当前待处理结点
//统计链表结点数
int count = 0;
while(p_after){
count++;
p_after = p_after->next;
}
//给返回数组分配空间
int* result = (int*)malloc(sizeof(int)*(count+1));
memset(result,0,sizeof(int));//只初始化一个0?
*returnSize = count;
count = 0;
// while(count < *returnSize){
// printf("%d,",result[count++]);
// }
// count = 0;
while(curNode != NULL){//遍历链表
//寻找第一个大与curNode的结点
p_after = curNode->next;
while(p_after != NULL && p_after->val <= curNode->val){
p_after = p_after->next;
}
//如果存在下一个更大节点,则将其值赋值给返回数组;不存在将返回数组置0
if(p_after != NULL){
result[count++] = p_after->val;
}
else{
result[count++] = 0;
}
//处理下一个结点,直到遍历完所有结点
curNode = curNode->next;
}
return result;
}
法二:
/*第二次ac:
执行用时 : 208 ms, 在Next Greater Node In Linked List的C提交中击败了100.00% 的用户
内存消耗 : 34.3 MB, 在Next Greater Node In Linked List的C提交中击败了88.89% 的用户
*/
/*第一次ac:
执行用时 : 372 ms, 在Next Greater Node In Linked List的C提交中击败了84.38% 的用户
内存消耗 : 34.4 MB, 在Next Greater Node In Linked List的C提交中击败了88.89% 的用户
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* nextLargerNodes(struct ListNode* head, int* returnSize){
//特殊情况
if(head == NULL){
return NULL;
}
//计算链表长度
struct ListNode* p = head;
*returnSize = 0;
while(p){
(*returnSize)++;
p = p->next;
}
//定义返回数组
int* result = (int*)malloc(sizeof(int)*(*returnSize));
int* val = (int*)malloc(sizeof(int)*(*returnSize));//用于保存链表结点的值
int i = 0,j = 0;
struct ListNode* curNode = head;
while(curNode){
result[i] = 0;
val[i] = curNode->val;//当前结点值赋值给val[]数组
i++;
//j从i-2开始,本来是i-1开始,但上面已经对i++自加了1,所有这里为i-2
for(j = i-2; j >= 0;j--){
//当前面的结点未找到下一个最大值(result == 0)并且其值比当前结点curNode小(val[i] < curNode->val)时,则代表其找到了下一个最大结点,修改结果数组result[j] = curNode->val
if(result[j] == 0 && val[j] < curNode->val){
result[j] = curNode->val;
}
//前面有结点大于当前结点curNode的值时,代表curNode不再可能是下一个更大结点
if(val[j] >= curNode->val){
break;
}
}//for
curNode = curNode->next;
}//while
return result;
}
后记:
- 法三:利用栈。栈保存结点下标。返回结果数组保存结点值(后续修改成下一个更大节点)。从前往后遍历链表并将结点下标进栈,将结点值赋值给返回结果数组。如果栈不空且栈顶元素小于当前结点,则出栈栈顶元素并将其下一个更大结点记为当前结点,直到链表为NULL。当链表遍历到最后一个结点但栈empty时,出栈所有元素,其下一个更大结点为0.
- 法四:动态规划。从后往前遍历,并分三种情况:如果当前结点小于其后继结点,则下一个更大结点就是其后继。如果当前结点等于其后继结点,则下一个更大结点就是其后继的下一个更大结点(由于从后向前处理,所以其后继结点的下一个更大节点是已知的)。如果当前结点大于其后继结点,则依次比较其后面的所有结点,得出其下一个更大结点。