链表问题集锦

1.单链表的初始化,输出以及插入删除的基本操作

#include <iostream>
#include <string>
using namespace std;
    
struct Node{
   int data;
   Node *next;
};

Node* IniList()
{
    Node *p,*head=NULL;
    for(int i=10;i>0;--i)
    {
        p=(Node*)malloc(sizeof(Node));
        p->data=i;
        p->next=head;
        head=p;
    }
    return head;
}
void PrintList(Node *head)
{
    Node *s=head;
    while(s->next!=NULL)
    {
        cout<<s->data<<" ";
        s=s->next;
    }
    cout<<s->data;
    cout<<endl;
}

bool InsertList(Node *head,int n,int data)
{
    Node *p=head;
    if(!p) return false;
    int i=1;
    while(p->next!=NULL && i<n)
    {
        p=p->next;
        ++i;
    }
    if(!(p->next)) return false;

    Node *s;
    s=(Node*)malloc(sizeof(Node));
    s->data=data;
    s->next=p->next;
    p->next=s;
    return true;
}

void DeletList(Node* head,int n)
{
    Node* p=head,*q;
    int i=1;
    while(p && p->next && i<n-1)
    {
        p=p->next;
        ++i;
    }
    q=p->next;
    p->next=q->next;
    free(q);
}

int main()
{
    Node *head;
    head=IniList();
    PrintList(head);
    if(InsertList(head,5,13))
        PrintList(head);
    DeletList(head,4);
    PrintList(head);
    return 0;
}

2.在O(1)时间删除链表节点

bool DeleteElem(Node *cur)
{
    if(!cur || !cur->next) return false;
    Node *p;
    p=cur->next;
    cur->data=p->data;
    cur->next=p->next;
    free(p);
    return true;
}

int main()
{
    Node *head;
    head=IniList();
    PrintList(head);
    Node *cur=head;
    int m=3;
    while(m--)
    {
        cur = cur->next;
    }
    if(DeleteElem(cur))
        PrintList(head);
    return 0;
}

3.反转单链表

Node* ReverseList(Node* head)
{
    Node *pre=NULL,*next=NULL;
    if(!head || !head->next) return head;
    while(head!=NULL)
    {
        next=head->next;
        head->next=pre;
        pre=head;
        head=next;
    }
    return pre;
}


int main()
{
    Node *head;
    head=IniList();
    PrintList(head);
    
    head=ReverseList(head);
    PrintList(head);
    return 0;
}

4.求链表倒数第k个节点

Node* theKthNode(Node* head,int k)
{
    Node *slow=head,*fast=head;
    int i;
    for(i=k;i>0 && fast->next!=NULL;--i)
        fast=fast->next;
    if(i>0) return NULL;
    
    while(fast!=NULL)
    {
        fast=fast->next;
        slow=slow->next;
    }
    return slow;
}

5.求链表中间节点

Node* theMiddleNode(Node* head)
{
    Node* slow=head,*fast=head;
    if(head==NULL) return NULL;
    if(head->next==NULL) return head;

    while(fast!=NULL && fast->next!=NULL)
    {
        fast=fast->next->next;
        slow=slow->next;
    }
    return slow;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【声明】欢迎转载,但请保留文章原始出处→_→文章来源:http://www.jianshu.com/p/08d08...
    梦工厂阅读 3,786评论 3 31
  • 1 序 2016年6月25日夜,帝都,天下着大雨,拖着行李箱和同学在校门口照了最后一张合照,搬离寝室打车去了提前租...
    RichardJieChen阅读 5,148评论 0 12
  • 转载请注明出处:http://www.jianshu.com/p/c65d9d753c31 在上一篇博客《数据结构...
    Alent阅读 3,528评论 4 74
  • //leetcode中还有花样链表题,这里几个例子,冰山一角 求单链表中结点的个数----时间复杂度O(n)这是最...
    暗黑破坏球嘿哈阅读 1,533评论 0 6
  • 大学的时候不好好学习,老师在讲台上讲课,自己在以为老师看不到的座位看小说,现在用到了老师讲的知识,只能自己看书查资...
    和珏猫阅读 1,471评论 1 3