文章目录
链表分割
链接: 链表分割
下面是带哨兵位的实现
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
struct ListNode *cur=pHead;
struct ListNode *head1,*head2,*tail1,*tail2;//1的是小值,2的是大值
//开辟哨兵位
head1=tail1=(struct ListNode *)malloc(sizeof(struct ListNode ));
head2=tail2=(struct ListNode *)malloc(sizeof(struct ListNode ));
while(cur){
struct ListNode *next=cur->next;
if(cur->val<x){
tail1->next=cur;
tail1=tail1->next;
tail1->next=NULL;
}
else{
tail2->next=cur;
tail2=tail2->next;
tail2->next=NULL;
}
cur=next;
}
//把两个链表接到一起,如果第一个链表为空也无所谓
tail1->next=head2->next;
struct ListNode *head=head1->next;
free(head1);//没用的就free掉
free(head2);
return head;
}
};
下面是不带哨兵位的实现
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
struct ListNode *cur=pHead;
struct ListNode *head1=NULL,*head2=NULL,*tail1=NULL,*tail2=NULL;//一般都是定义两组指针,一组管理一个链表
while(cur){
struct ListNode *tmp=cur;
cur=cur->next;
if(tmp->val<x){
if(tail1==NULL){//如果链表为空
head1=tail1=tmp;
tail1->next=NULL;
}
else{
tail1->next=tmp;
tail1=tail1->next;
tail1->next=NULL;
}
}
else{
if(tail2==NULL){
head2=tail2=tmp;
tail2->next=NULL;
}
else{
tail2->next=tmp;
tail2=tail2->next;
tail2->next=NULL;
}
}
}
//合并两个链表
if(head1==NULL){//判断第一个表是否为空
return head2;
}
else{
tail1->next=head2;
return head1;
}
}
};
环形链表
链接:环形链表
下面我们来实现一下
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode *fast=head,*slow=head;
while(fast&&fast->next){//能出循环就没有环,有环在循环中返回
fast=fast->next->next;
slow=slow->next;
if(fast==slow){
struct ListNode *tmp=fast;
while(tmp!=head){//相同时停止,并返回这个点
tmp=tmp->next;
head=head->next;
}
return head;
}
}
return NULL;
}
下面我们来实现一下
//找相交点的函数
//思路就是计算两个链表的长度,长的先走差的长度数,最后同时走,相同了就找到了
struct ListNode *meetpoint(struct ListNode *s1,struct ListNode *s2){
int num1=0;
int num2=0;
struct ListNode *head1=s1,*head2=s2;
while(head1){
num1++;
head1=head1->next;
}
while(head2){
num2++;
head2=head2->next;
}
struct ListNode *thelong=s1,*theshort=s2;
if(num2>num1){
thelong=s2;
theshort=s1;
}
int num=abs(num1-num2);//求差的长度数
while(num){
thelong=thelong->next;
num--;
}
while(thelong!=theshort){
thelong=thelong->next;
theshort=theshort->next;
}
return thelong;
}
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode *fast=head,*slow=head;
while(fast&&fast->next){
struct ListNode *fastprev=fast->next;//记录一下fast的上个位置
fast=fast->next->next;
slow=slow->next;
if(fast==slow){
fastprev->next=NULL;
return meetpoint(head,slow);
}
}
return NULL;
}
有效的括号
链接:有效的括号
bool isValid(char * s){
ST st;
STInit(&st);
while(*s){
if(*s=='('||*s=='['||*s=='{'){//左括号入栈
STPush(&st,*s);
}
else{
if(STEmpty(&st)){//栈为空并且要处理的字符为右括号
STDestroy(&st);
return false;
}
char tmp=STTop(&st);//匹配栈顶元素和要处理的元素
if(*s==')'&&tmp!='('||*s==']'&&tmp!='['||*s=='}'&&tmp!='{'){
return false;
}
else{
STPop(&st);
}
}
s++;
}
if(!STEmpty(&st)){//true的话最后栈应该为空
STDestroy(&st);
return false;
}
else{
STDestroy(&st);
return true;
}
}