链表算法练习题

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

1.删除单链表的重复结点

力扣链接

//方法一:
struct ListNode* removeDuplicateNodes(struct ListNode* head)
{
    struct ListNode *current = head;
    while(current)
    {
        struct ListNode *p = current;
        while(p->next)
        {
            if (p->next->val == current->val)
            {
                p->next = p->next->next;
            }
            else
            {
                p = p->next;
            }
            
        }
        current = current->next;
    }
    return head;
}
//方法二:struct ListNode* removeDuplicateNodes(struct ListNode* head)
{
    int index[20001] = {0};//这里要有初始化,否则是垃圾数
    struct ListNode *pre = head;
    struct ListNode *current = pre->next;
    index[head->val] = 1;

    while (current)
    {
        if (index[current->val] == 0)
        {
            index[current->val] = 1;
            pre = current;
            current = current->next;
        }
        else
        {
            pre->next = current->next;
            current = pre->next;
        }
    }

    return head;
}

2.找出链表的倒数第K个元素

力扣链接

struct ListNode* getKthFromEnd(struct ListNode* head, int k)
{
    struct ListNode* fast = head;
    struct ListNode* slow = head;
    while(k--)
    {
        fast = fast->next;
    }
    while(fast)
    {
        fast = fast->next;
        slow = slow->next;
    }
    return slow;
}

3.找出链表的中间节点

力扣链接

struct ListNode* middleNode(struct ListNode* head)
{
    struct ListNode* fast= head;
    struct ListNode* slow= head;
    while(fast != NULL && fast->next !=NULL)
    {
        fast = fast->next->next;
        slow = slow->next;
    }
    return slow;
}
//如果是要返回两个中间结点的前者,判断条件应该为:
struct ListNode* middleNode(struct ListNode* head)
{
    struct ListNode* fast = head;
    struct ListNode* latter = head;
    while(fast->next != NULL && fast->next->next !=NULL)
    {
        fast = fast ->next->next;
        slow = slow->next;
    }
    return slow;
}

4.反转链表

力扣链接

struct ListNode* reverseList(struct ListNode* head)
{
    if(head == NULL)
    {
        return head;
    }
    struct ListNode* former = NULL;
    struct ListNode* mid = head;
    struct ListNode* latter = NULL;
    
    while(mid != NULL)
    {
        former = mid->next;
        mid->next = latter;
        latter = mid;
        mid = former;
    }
    return latter;
}

5.判断环形链表

力扣链接

bool hasCycle(struct ListNode *head) 
{
    if (NULL == head || NULL == head->next)
    {
        return false;
    }

    struct ListNode * slow=head;
    struct ListNode * fast=head->next;

    while (fast && fast->next)
    {
        if(slow == fast)
        {
            return true;
        }
        else
        {
            slow=slow->next;
            fast=fast->next->next;
        }
    }

    return false;
}

6.链表相交

力扣链接

//方法一:
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
{
    if(headA == NULL || headB == NULL)
    {
        return NULL;
    }
    int length_a = 1;
    int length_b = 1;
    int length = 0;
    struct ListNode *a = headA;
    struct ListNode *b = headB;

    while(a->next)
    {
        a = a->next;
        length_a++;
    }
    while(b->next)
    {
        b = b->next;
        length_b++;
    }
    if(&*a == &*b)
    {
        a = headA;
        b = headB;
        length = length_a - length_b;
        if (length > 0)
        {
            while(length--)
            {
                a = a->next;
            }
        }
        else
        {
            length = abs(length);
            while(length--)
            {
                b = b->next;
            }
        }
        while (&*a != &*b)
        {
            a = a->next;
            b = b->next;
        }
        return a;
    }
    else
    {
        return NULL;
    }
}
//方法二:
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
{
    struct ListNode *p=headA;
    struct ListNode *q=headB;
    while(p!=q)
    {
        if (p==NULL)
            p=headB;
        else
            p=p->next;
        if (q==NULL)
            q=headA;
        else
            q=q->next;
    }
    return q;
}

7.判断回文链表

力扣链接

bool isPalindrome(struct ListNode* head)
{
    struct ListNode *fast = head;
    struct ListNode *slow = head;

    /*找到中间结点*/
    while (fast && fast->next)
    {
        fast = fast->next->next;
        slow = slow->next;
    }
    struct ListNode *left = NULL;
    struct ListNode *current = slow;
    struct ListNode *right = NULL;
    /*后半段反转*/
    while (current != NULL)
    {
        right = current->next;
        current->next = left;
        left = current;
        current = right;
    }
    /*判断回文*/
    fast = head;
    while (left)
    {
        if(fast->val != left->val)
        {
            return false;
        }
        left = left->next;
        fast = fast->next;
    }
    return true;
}

8.合并两个有序链表

力扣链接

struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
{
    struct ListNode *a = l1;
    struct ListNode *b = l2;
    struct ListNode *head = malloc(sizeof(struct ListNode));
    struct ListNode *current = head;
    if (head == NULL)
    {
        return NULL;
    }

    while(a && b)
    {
        if(a->val <= b->val)
        {
            current->next = a;
            a = a->next;
        }
        else
        {
           current->next = b;
            b = b->next;
        }
        current = current->next;
    }
    current->next = (NULL == a ? b : a);

    return head->next;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值