单链表相关操作

结构定义:

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