链表常用方法

头插法:

struct LinkList List_HeadInsert(struct LinkList &L){
LNode *s;
int x;
L=(struct ListNode)malloc(sizeof(struct ListNode));
L->next=NULL;
while(x!=999){
s=(struct ListNode*)malloc(sizeof(struct ListNode));
s->data=x;
s->next=L->next;
L->next=s;
scanf("%d",&x);
}
return L;
}

尾插法:

struct ListNode List_TailInsert(struct ListNode &L){
int x;
L=(struct ListNode)malloc(struct ListNode);
LNode *s,*r=L;
while(x!=999){
s=(struct ListNode)malloc(struct ListNode);
s->data=x;
r->next=s;
r=s;
scanf("%d",&x);
}
return L;
}

逆置法:

struct ListNode * reverseList(struct ListNode &L){
if(head==NULL||head->next==NULL)
return head;
struct ListNode *prev=NULL;
struct ListNode *cur=head;
while(cur){
struct ListNode *curp=cur->next;
cur->next=prev;
prev=cur;
cur=curp;
}
return prev;
{

归并法:

​
struct node *sumw(struct node *x1,struct node *x2 )
{undefined
    struct node *p,*q;
    struct node *tail;
    p=x1->next;
    q=x2->next;
    tail=x1;
    free(x2);//将链表2的头链表释放
    while (p&&q) {undefined
        if (p->data < q->data) {
            tail->next=p;
            tail=p;
            p=p->next;
        }else
        {
            tail->next=q;
            tail=q;
            q=q->next;
        }
    }
    if (p)
        tail->next=p;
    else
    
        tail->next=q;
    
    
    return x1;
}

​

双指针:

struct ListNode* middleNode(struct ListNode* head){    
	struct ListNode* fast, *slow;    
	fast = slow =head;    
	while(fast && fast->next){        
		slow = slow->next;        
		fast = fast->next->next;    
	}    
return slow;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值