美文网首页
链表问题集锦

链表问题集锦

作者: nino天 | 来源:发表于2014-09-14 21:28 被阅读57次

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;
}

相关文章

  • 链表问题集锦

    1.单链表的初始化,输出以及插入删除的基本操作 2.在O(1)时间删除链表节点 3.反转单链表 4.求链表倒数第k...

  • 面试精选之链表问题集锦

    链表问题是面试过程中经常被问到的一部分,很考查编程功底。最近刷了 LeetCode 上链表部分的面试题,我总结了一...

  • 单链表

    单链表一些相关的算法集锦,单链表的算法可以提高逻辑能力。 反转链表 最基本的链表的题目,很简单的迭代操作,需要注意...

  • 一些面试题

    算法与数据结构 1、链表问题集锦 2、快排 3、2个有序数组如何成为1个有序数组 4、找出字串中最长连续 5、多线...

  • 1.数据结构-链表问题

    链表相关问题 删除节点 链表去重 有环链表 反转链表 链表排序 链表相交 其他问题 面试题 02.03. 删除中间...

  • LeetCode 探索中级 [俩数相加][奇偶链表][相交链表]

    链表 @[链表|双指针] 链表问题相对容易掌握。 不要忘记"双指针解法",它不仅适用于数组问题,而且还适用于链表问...

  • 问题集锦

    公司一 在两个子线程中利用Handler 实现通信 手写冒泡算法 Activity 四种启动模式 Activity...

  • 问题集锦

    (intermediate value)(...) is not a function https://githu...

  • 问题集锦

    1. Release apk打包问题:Lint found fatal errors while assembli...

  • 问题集锦

    一伙人凭借激情凑在一起开店容易,但是想要经营好并非易事。 第一:股东众多、管理混乱且低效。众筹模式少则几十人,多则...

网友评论

      本文标题:链表问题集锦

      本文链接:https://www.haomeiwen.com/subject/yexutttx.html