单链表相关操作

结构定义:

struct ListNode {
    int val;
    ListNode *next;
};

1. 头插法创建链表 

ListNode* createListFromHead()
{
    ListNode *list = new ListNode;
    list->next = NULL;
    ListNode *s;
    int x;
    while (cin >> x)
    {
        s = new ListNode;
        s->val = x;
        s->next = list->next;
        list->next = s;
    }
    return list;
}
View Code

  2. 尾插法创建链表 

ListNode* createListFromTail()
{
    ListNode *list = new ListNode;
    list->next = NULL;
    ListNode *s;
    int x;
    ListNode *p = list;
    while (cin >> x)
    {
        s = new ListNode;
        s->val = x;
        p->next = s;
        p = s;
    }
    p->next = NULL;
    return list;
}
View Code

  3. 查找结点 

ListNode* searchNode(ListNode *head, int x)
{
    ListNode *p = head;
    while (p != NULL && p->val != x)
        p = p->next;
    return p;
}
View Code

  4. 添加结点 

void addToTail(ListNode **head, int x)
{
    ListNode *s = new ListNode;
    s->val = x;
    s->next = NULL;
    if(*head == NULL)
        *head = s;
    else
    {
        ListNode *p = *head;
        while(p->next != NULL)
            p = p->next;
        p->next = s;
    }
}
View Code

  5. 删除结点 

void removeNode(ListNode **head, int x)
{
    if(head == NULL || *head == NULL)
        return;
    ListNode *pd = NULL;
    if ((*head)->val == x)
    {
        pd = *head;
        *head = (*head)->next;
    }
    else
    {
        ListNode *p = *head;
        while (p->next != NULL && p->next->val != x)
            p = p->next;
        if (p->next != NULL && p->next->val == x)
        {
            pd = p->next;
            p->next = pd->next;
        }
    }
    if(pd != NULL)
        delete pd;
}
View Code

 经典面试题:

2. 查找倒数第K个结点

ListNode* getKthNode(ListNode *head, int k)
{
    if(k < 1 || head == NULL)
        return NULL;
    ListNode *fast = head;
    ListNode *slow = head;
    for (int i = 0;i < k-1;++i)
    {
        if(fast->next != NULL)
            fast = fast->next;
        else
            return NULL;
    }
    while (fast->next != NULL)
    {
        fast = fast->next;
        slow = slow->next;
    }
    return slow;
}
View Code

4. 合并有序单链表

5. 判断单链表是否有环

bool isExistLoop(ListNode *head)
{
    ListNode *fast = head;
    ListNode *slow = head;
    while (fast != NULL && fast->next != NULL)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast)
            return true;
    }
    return false;
}
View Code

6. 查找单链表中环的入口结点

ListNode* findLoopPort(ListNode *head)
{
    ListNode *fast = head;
    ListNode *slow = head;
    while (fast != NULL && fast->next != NULL)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast)
            break;
    }
    if (fast == NULL || fast->next == NULL)
        return NULL;
    slow = head;
    while (slow != fast)
    {
        slow = slow->next;
        fast = fast->next;
    }
    return slow;
}
View Code

7. 求两个单链表的相交结点

 

 

参考资料

    轻松搞定面试中的链表题目

    面试精选:链表问题集锦

转载于:https://www.cnblogs.com/gattaca/p/4415591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值