1019.链表中的下一个更大节点
(1)两层循环
/**
* 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;
int* res=(int*)malloc(sizeof(int)*10000);
memset(res,0,sizeof(res));
// 两次循环
struct ListNode* cur=head,*move=head;
int len=0;
while(cur){
move=cur->next;
while(move && move->val <= cur->val){
move=move->next;
}
res[len]=move ? move->val : 0;
len++;
cur=cur->next;
}
*returnSize=len;
return res;
}
(2)单调栈
用栈来存储还没有找到下一个更大节点的元素的位置
/**
* 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;
// 单调栈
int* res=(int*)malloc(sizeof(int)*10000);
struct ListNode* p=head;
int len=0;
while(p){
res[len]=p->val;
p=p->next;
len++;
}
int top=-1;
int* stack=(int*)malloc(sizeof(int)*len);
//这里stack存的是栈顶元素在数组中的位置
*returnSize=len;
for(int i=0;i<len;i++)
{
while(top!=-1 && res[stack[top]]<res[i]){
res[stack[top]]=res[i];
top--;
}
top++;
stack[top]=i;
}
while(top!=-1){
res[stack[top]]=0;
top--;
}
return res;
}
19. 删除链表的倒数第N个节点
(1)先求出长度,找倒数的n个节点的前一个节点,常规做法
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
if(head==NULL) return head;
struct ListNode* p=head;
int len=0;
while(p){
len++;
p=p->next;
}
p=head;
int index=0;
if(n==len) return head->next;
while(index+n < len-1){
p=p->next;
index++;
}
p->next=p->next->next;
return head;
}
(2)一遍扫描
两个指针,快的指针先走n步
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
// 一趟扫描,两个指针
struct ListNode* p=head,*q=head;
while(q && n--){
q=q->next;
}
if(q==NULL) return head->next;
while(q->next){
p=p->next;
q=q->next;
}
p->next=p->next->next;
return head;
}
24.两两交换链表中的节点
(1)非递归
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* swapPairs(struct ListNode* head){
if(head==NULL || head->next==NULL){
return head;
}
struct ListNode*cur,*nextnode;
//非递归
struct ListNode* prev=(struct ListNode*)malloc(sizeof(struct ListNode));
prev->next=head;
struct ListNode* temp=prev;
while(temp->next!=NULL && temp->next->next!=NULL){
cur=temp->next;
nextnode=temp->next->next;
temp->next=nextnode;
cur->next=nextnode->next;
nextnode->next=cur;
temp=cur;
}
return prev->next;
}
(2)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* swapPairs(struct ListNode* head){
if(head==NULL || head->next==NULL){
return head;
}
struct ListNode* nextnode=head->next;
// 递归
head->next=swapPairs(nextnode->next);
nextnode->next=head;
head=nextnode;
return head;
}