LeetCode刷题3(链表OJ题)

1.两个链表的第一个公共节点

 

typedef struct ListNode Node;
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{
    int l1 = 0;
    int l2 = 0;
    Node* cur1 = headA;
    Node* cur2 = headB;

    //计算各自的结点数
    while(cur1)
    {
        l1++;
        cur1 = cur1->next;
    }
    while(cur2)
    {
        l2++;
        cur2 = cur2->next;
    }

    //默认长结点为headA,短结点为headB,如果不对,再交换
    Node* longList = headA;
    Node* shortList = headB;
    if(l1 < l2)
    {
        longList = headB;
        shortList = headA;
    }

    int gap = abs(l1 - l2);
    while(gap--)
    {
        longList = longList->next;
    }

    cur1 = longList;
    cur2 = shortList;

    while(cur1)
    {
        if(cur1 == cur2)
        {
            return cur1;
        }
        else
        {
            cur1 = cur1->next;
            cur2 = cur2->next;
        }
    }

    return NULL;
}

2.环形链表

该题需要用到双指针 由该题再问大家一个问题,fast必须一次走两步吗?能不能一次走三步?四步?或者跟多步?

typedef struct ListNode Node;
bool hasCycle(struct ListNode *head) 
{
    if(head == NULL || head->next == NULL)
        return false;
    
    Node* slow = head;
    Node* fast = head;

    while(fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;

        if(slow == fast)
            return true;
    }

    return false;
}

3.环形链表 II

 

typedef struct ListNode Node;
struct ListNode *detectCycle(struct ListNode *head) 
{
    if(head == NULL || head->next == NULL)
        return NULL;
    
    Node* slow = head;
    Node* fast = head;

    while(fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;

        if(fast == slow)
            break;
    }

    if(fast == NULL || fast->next == NULL)
        return NULL;
    
    while(head != slow)
    {
        head = head->next;
        slow = slow->next;
    }

    return head;
}

4.对链表进行插入排序

typedef struct ListNode Node;
struct ListNode* insertionSortList(struct ListNode* head)
{
    if(head == NULL || head->next == NULL)
        return head;
    
    Node* sortHead = head;
    Node* cur = head->next;
    sortHead->next = NULL;

    while(cur)
    {
        Node* next = cur->next;
        //头插
        if(cur->val <= sortHead->val)
        {
            cur->next = sortHead;
            sortHead = cur;
        }
        else
        {
            Node* sortprev = sortHead;
            Node* sortcur = sortHead->next;   
            //中间插
            while(sortcur)
            {
                if(cur->val <= sortcur->val)
                {
                    sortprev->next = cur;
                    cur->next = sortcur;
                    break;
                }
                else
                {
                    sortprev = sortcur;
                    sortcur = sortcur->next;
                }
            }

            //尾插
            if(sortcur == NULL)
            {
                sortprev->next = cur;
                cur->next = NULL;
            }
        }

        cur = next;
    }

    return sortHead;
}

5.复制带随机指针的链表 

 

typedef struct Node Node;
struct Node* copyRandomList(struct Node* head) 
{
    if(head == NULL)
        return head;
    
    Node* cur = head;

    while(cur)
    {
        Node* next = cur->next;

        Node* copy = (Node*)malloc(sizeof(Node));
        copy->next = NULL;
        copy->random = NULL;

        copy->val = cur->val;
        copy->next = cur->next;
        cur->next = copy;

        cur = next;
    }

    cur = head;
    Node* copyCur = cur->next;

    while(cur)
    {
        Node* next = copyCur->next;

        if(cur->random)//cur中的random为空则直接将复制节点中的random置空
        {
            copyCur->random = cur->random->next;
        }
        else
        {
            copyCur->random = NULL;
        }
        
        cur = next;
        if(next)
            copyCur = cur->next;
    }

    cur = head;
    copyCur = cur->next;
    Node* copyHead = copyCur;

    while(cur)
    {
        Node* next = copyCur->next;

        cur->next = next;
        if(next)
            copyCur->next = next->next;
        else
            copyCur->next = NULL;
        
        cur = next;

        if(next)
            copyCur = cur->next;
    }

    return copyHead;
}

6.删除链表中重复的结点 

typedef struct ListNode Node;
struct ListNode* deleteDuplication(struct ListNode* pHead ) 
{
    if(pHead == NULL || pHead->next == NULL)
        return pHead;
    
    Node* prev = NULL;
    Node* cur = pHead;

    while(cur)
    {
        Node* next = cur->next;

        if(next == NULL)
        {
            return pHead;
        }

        if(cur->val != next->val)
        {
            prev = cur;
            cur = next;
        }
        else
        {
            while(next && next->val == cur->val)
            {
                next = next->next;
            }
            if(prev)
            {
                prev->next = next;
            }
            else
            {
                pHead = next;
            }
            while(cur != next)
            {
                Node* destroy = cur;
                cur = cur->next;
                free(destroy);
            }
        }
    }

    return pHead;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值