0
点赞
收藏
分享

微信扫一扫

Leecode链表OJ集合

这个博客主要是我这几天做的链表题,这两天想给自己加一个强训。这篇只有代码,没有任何讲解。下面这些代码都是在OJ上跑过的。有些代码可能比较挫。后面我看看自己能不能优化优化。由于代码没有注释,理解起来可能有些吃力,后面等我做够了题目选择典型的题和大家分享一下啊。要是对代码有疑惑的同学也可以私信我.

除了一些需要会员和题目与二叉树以及哈希相关的,下面大该是Leetcode中链表容易的部分的全部的内容了。大家可以看看

@toc

Leetcode 19. 删除链表的倒数第 N 个结点

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

struct ListNode* removeNthFromEnd(struct ListNode* head, int n)
{
    if(head == NULL)
    {
        return NULL;
    }
    struct ListNode* fast = head;
    struct ListNode* slow = head;
    while(n--)
    {
        if(fast == NULL)
        {
            return NULL;
        }
        fast = fast->next;
    }
    struct ListNode* prev = NULL; 
    while(fast != NULL)
    {
        fast = fast->next;
        prev = slow;
        slow = slow->next;
    }
    if(prev == NULL)
    {

        head = head->next;
        return head;
    }

    prev->next = slow->next;
    slow->next = NULL;
    free(slow);
    return head;
}

Leetcode 剑指 Offer 06. 从尾到头打印链表

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

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* reversePrint(struct ListNode* head, int* returnSize)
{
    if(head == NULL)
    {
        *returnSize = 0;
        return NULL;
    }
    int count = 0;
    struct ListNode* cur = head;
    while(cur != NULL)
    {
        cur = cur->next;
        count++;
    }
     *returnSize = count;
    int* p = (int*)malloc(sizeof(int)*count);
    cur = head;
    while(cur != NULL)
    {
        p[--count] = cur->val;
        cur = cur->next;
    }
    return p;
}

Leetcode 83. 删除排序链表中的重复元素

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

struct ListNode* deleteDuplicates(struct ListNode* head)
{
    if(head == NULL)
    {
        return NULL;
    }
    struct ListNode* prev = head;
    struct ListNode* cur = head->next;
    while(cur!=NULL)
    {
        if(cur->val != prev->val)
        {
           prev->next = cur;
           prev = cur;
        }

        cur = cur->next;
    }
    prev->next = NULL;
    return head;
}

Leetcode 剑指 Offer 18. 删除链表的节点

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

struct ListNode* deleteNode(struct ListNode* head, int val)
{
    if(head == NULL)
    {
        return NULL;
    }
    struct ListNode* cur = head->next;
    struct ListNode* prev = head;
    if(head->val == val)
    {
        free(head);
        head = cur;
        return head;
    }
    while(cur != NULL)
    {
        if(cur->val == val)
        {
            break;
        }
        prev = cur;
        cur = cur ->next;
    }
    prev->next = cur->next;
    free(cur);
    return head;
}

Leetcode 剑指 Offer 24. 反转链表 & 剑指 Offer II 024. 反转链表

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

struct ListNode* reverseList(struct ListNode* head)
{
    if(head == NULL)
    {
        return NULL;
    }
    struct ListNode* newHead = NULL;
    struct ListNode* cur = head;
    while(cur != NULL)
    {
        struct ListNode* next = cur->next;
        if(newHead == NULL)
        {
            newHead = cur;
            newHead->next = NULL;
        }
        else
        {
            cur->next = newHead;
            newHead = cur;
        }

        cur = next;
    }
    return newHead;
}

Leetcode 剑指 Offer 25. 合并两个排序的链表

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

struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
{
    if(l1 == NULL)
    {
        return l2;
    }
    if(l2 == NULL)
    {
        return l1;
    }
    struct ListNode* newHead = NULL;
    struct ListNode* tail = NULL;
    while(l1 != NULL && l2 != NULL)
    {
        if(l1->val < l2->val)
        {
            if(newHead == NULL)
            {
                newHead = l1;
                tail = newHead;
            }
            else
            {
                tail->next = l1;
                tail = l1;
            }
            l1 = l1->next;
        }
        else
        {
            if(newHead == NULL)
            {
                newHead = l2;
                tail = newHead;
            }
            else
            {
                tail->next = l2;
                tail = l2;
            }
            l2 = l2->next;
        }
    } 

    if(l1 != NULL)
    {
        tail->next = l1;
    }
    if(l2 != NULL)
    {
        tail->next = l2;
    }
    return newHead;
}

Leetcode 面试题 02.07. 链表相交 & 剑指 Offer 52. 两个链表的第一个公共节点 & 剑指 Offer II 023. 两个链表的第一个重合节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{
    if(headA == NULL || headB == NULL)
    {
        return NULL;
    }
    int lenA = 1;
    int lenB = 1;
    struct ListNode * curA  = headA;
    struct ListNode * curB  = headB;
    while(curA->next != NULL)
    {
        lenA++;
        curA = curA->next;
    }
    while(curB->next != NULL)
    {
        lenB++;
        curB = curB->next;
    }

    struct ListNode * longNode = headA;
    struct ListNode * shortNode = headB;
    int len = lenA - lenB;
    if(lenA < lenB)
    {
        longNode = headB;
        shortNode = headA;
        len = -len;
    }

    while(len--)
    {
         longNode =  longNode->next;
    }

    while(longNode != NULL && shortNode != NULL)
    {
        if(longNode == shortNode)
        {
            return longNode;
        }
        longNode = longNode->next;
        shortNode = shortNode->next;
    }
    return NULL;
}

Leetcode 剑指 Offer II 027. 回文链表 & 234. 回文链表 & 面试题 02.06. 回文链表

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

struct ListNode* middleNode(struct ListNode* head)
{
    if (head == NULL)
    {
        return NULL;
    }
    struct ListNode* slow = head;
    struct ListNode* fast = head;
    while (fast != NULL && fast->next!=NULL)
    {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}

struct ListNode* reverseList(struct ListNode* head)
{
    if(head == NULL)
    {
        return NULL;
    }
    struct ListNode* pHead = NULL;
    struct ListNode* cur = head;
    while(cur != NULL)
    {
        if(pHead == NULL)
        {
            pHead = cur;
            cur = cur->next;
            pHead->next = NULL;
            continue;
        }
        struct ListNode* n = cur->next;
        cur->next = pHead;
        pHead = cur;
        cur = n;
    }
    return pHead;
}

bool isPalindrome(struct ListNode* head)
{
    struct ListNode* mid = middleNode(head);
    struct ListNode* cur = reverseList(mid);
    while(cur != NULL)
    {
        if(head->val != cur->val)
        {
            return false;
        }
        head = head->next;
        cur = cur->next;
    }
    return true;
}

Leetcode 面试题 02.02. 返回倒数第 k 个节点

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

int kthToLast(struct ListNode* head, int k)
{
    if(head == NULL)
    {
        return NULL;
    }
    struct ListNode* fast = head;
    struct ListNode* slow = head;
    while(k--)
    {
        if(fast == NULL || k < 0)
        {
            return NULL;
        }
        fast = fast->next;
    }
    while(fast != NULL)
    {
        fast = fast->next;
        slow = slow->next;
    }
    return slow->val;
}

Leetcode 面试题 02.03. 删除中间节点 & 237. 删除链表中的节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
void deleteNode(struct ListNode* node) 
{
    if(node == NULL)
    {
        return;
    }
    struct ListNode* cur = node;
    struct ListNode* next = cur->next;
    while(next != NULL)
    {
        cur->val = next->val;

        if(next->next == NULL)
        {
            cur->next = NULL;
        }
        cur = cur->next;

        next = next->next;
    }
}

Leetcode 面试题 02.01. 移除重复节点

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

int isTrue(struct ListNode* head, struct ListNode* tail,int val)
{
    if (head == NULL)
    {
        return 0;
    }
    struct ListNode* cur = head;
    tail->next = NULL;
    while (cur != NULL)
    {
        if (cur->val == val)
        {
            return 0;
        }
        cur = cur->next;
    }
    return 1;
}

struct ListNode* removeDuplicateNodes(struct ListNode* head){
    if(head == NULL)
    {
        return NULL;
    }
    struct ListNode* newHead = NULL;
    struct ListNode* tail = NULL;
    struct ListNode* cur = head;
    while(cur != NULL)
    {
        if(newHead == NULL)
        {
            newHead = cur;
            tail = newHead;
        }
        else
        {
            if(isTrue(newHead, tail,cur->val) == 1)
            {
                tail->next = cur;
                tail = cur;
            }
        }
        cur = cur->next;
    }
    tail->next = NULL;
    return newHead;
}
举报

相关推荐

0 条评论