【数据结构初阶】链表OJ

链表OJ

题目一:移除链表元素

OJ

方案一:

题目解析:

c 复制代码
代码演示:
struct ListNode
{
    int val;
    struct ListNode* next;
};
struct ListNode* removeElements(struct ListNode* head, int val)
{
    struct ListNode* cur = head, * prev = NULL;
    while (cur)
    {
        if (cur->val == val)
        {
            if (cur == head)
            {
                head = cur->next;
                free(cur);
                cur = head;
            }
            else
            {
                prev->next = cur->next;
                free(cur);
                cur = prev->next;
            }
        }
        else
        {
            prev = cur;
            cur = cur->next;
        }
    }
    return head;
}

方案二:

题目解析:把原链表遍历一遍,插入新链表

c 复制代码
struct ListNode* removeElements(struct ListNode* head, int val)
{
    struct ListNode* newnode = NULL, * tail = NULL;
    struct ListNode* cur = head;
    while (cur)
    {
        if (cur->val == val)
        {
            struct ListNode* del = cur;
            cur = cur->next;
            free(del);
        }
        else
        {
            if (tail == NULL)
            {
                newnode = tail = cur;
                //tail = tail->next;
            }
            else
            {
                tail->next = cur;
                tail = tail->next;
            }
            cur = cur->next;
        }
    }
    if (tail)
        tail->next = NULL;
    return newnode;
}

题目二:反转链表

OJ

题目解析:

c 复制代码
代码演示:
struct ListNode
{
    int val;
    struct ListNode* next;
};
struct ListNode* reverseList(struct ListNode* head) 
{
    struct ListNode* newnode = NULL, * cur = head;
    while (cur)
    {
        struct ListNode* next = cur->next;
        //尾插
        cur->next = newnode;
        newnode = cur;
        cur = next;
    }
    return newnode;
}

题目三:链表的中间节点

OJ

题目解析:

c 复制代码
代码演示:
struct ListNode 
{
    int val;
    struct ListNode* next;
};
struct ListNode* middleNode(struct ListNode* head)
{
    struct ListNode* fast = head, * slow = head;
    while (fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}

题目四:链表中倒数第k个结点

OJ

题目解析:

c 复制代码
代码演示:
struct ListNode
{
    int val;
    struct ListNode* next;
};
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k) 
{
    struct ListNode* fast = pListHead, * slow = pListHead;
    while (k--)
    {
        if (fast == NULL)
            return NULL;
        fast = fast->next;
    }
    while (fast)
    {
        slow = slow->next;
        fast = fast->next;
    }
    return slow;
}

题目五:合并两个有序链表

OJ

题目解析:

c 复制代码
代码演示:
struct ListNode 
{
    int val;
    struct ListNode* next;
};
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
{
    if (list1 == NULL)
        return list2;
    if (list2 = NULL)
        return list1;
    struct ListNode* tail = NULL, * head = NULL;
    head = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
    while (list1 && list2)
    {
        if (list1->val < list2->val)
        {
            tail->next = list1;
            tail = tail->next;
            list1 = list1->next;
        }
        else
        {
            tail->next = list2;
            tail = tail->next;
            list2 = list2->next;
        }
    }
    if (list1)
        tail->next = list1;
    if (list2)
        tail->next = list2;
    struct ListNode* del = head;
    head = head->next;
    free(del);
    return head;
}

题目六:链表分割

OJ

题目解析:

c 复制代码
代码演示:
struct ListNode 
{
    int val;
    struct ListNode* next;
    ListNode(int x) : val(x), next(NULL) {}
}; 
class Partition
{
public:
    ListNode* partition(ListNode* pHead, int x) 
    {
        struct ListNode* lhead, * ltail, * gtail, * ghead;
        ghead = gtail = (struct ListNode*)malloc(sizeof(struct ListNode));
        lhead = ltail = (struct ListNode*)malloc(sizeof(struct ListNode));
        struct ListNode* cur = pHead;
        while (cur)
        {
            if (cur->val < x)
            {
                ltail->next = cur;
                ltail = ltail->next;
            }
            else
            {
                gtail->next = cur;
                gtail = gtail->next;
            }
            cur = cur->next;
        }
        ltail->next = ghead->next;
        gtail->next = NULL;
        struct ListNode* head = lhead->next;
        free(lhead);
        free(ghead);
        return head;
    }
};

题目七:链表的回文结构

OJ

题目解析:

c 复制代码
代码演示:
struct ListNode 
{
	int val;
	struct ListNode* next;
	ListNode(int x) : val(x), next(NULL) {}
}; 
class PalindromeList
{
public:
	struct ListNode* middleNode(struct ListNode* head)
	{
		struct ListNode* fast = head, * slow = head;
		while (fast && fast->next)
		{
			slow = slow->next;
			fast = fast->next->next;
		}
		return slow;
	}
	struct ListNode* reverseList(struct ListNode* head)
	{
		struct ListNode* newnode = NULL, * cur = head;
		while (cur)
		{
			struct ListNode* next = cur->next;
			cur->next = newnode;
			newnode = cur;
			cur = next;
		}
		return newnode;
	}
	bool chkPalindrome(ListNode* A)
	{
		struct ListNode* mid = middleNode(A);
		struct ListNode* rmid = reverseList(mid);
		while (A && rmid)
		{
			if (A->val != rmid->val)
				return false;
			A = A->next;
			rmid = rmid->next;
		}
		return true;
	}
};

题目八:相交链表

OJ

题目解析:

定义快慢指针,使快指针先走与慢指针同步。然后同时走看是否相交

c 复制代码
代码演示:
struct ListNode
{

    int val;
    struct ListNode* next;
};
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) 
{
    struct ListNode* curA = headA, * curB = headB;
    int lenA = 1, lenB = 1;
    while (curA)
    {
        curA = curA->next;
        lenA++;
    }
    while (curB)
    {
        curB = curB->next;
        lenB++;
    }
    if (curA != curB)
        return false;
    int gab = abs(lenA - lenB);
    struct ListNode* longest = headA, * shortest = headB;
    if (lenA < lenB)
    {
        longest = headB;
        shortest = headA;
    }
    while (gab--)
    {
        longest = longest->next;
    }
    while (shortest != longest)
    {
        
        shortest = shortest->next;
        longest = longest->next;
    }
    return longest;
}

题目九:环形链表

OJ

题目解析:

c 复制代码
代码演示:
struct ListNode {
    int val;
    struct ListNode* next;
};
bool hasCycle(struct ListNode* head)
{
    struct ListNode* fast = head, * slow = head;
    while (fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast)
            return true;
    }
    return false;
}

题目十:环形链表II

OJ

题目解析:

c 复制代码
代码演示:
struct ListNode
{
    int val;
    struct ListNode* next;
};
struct ListNode* detectCycle(struct ListNode* head)
{
    struct ListNode* fast = head, * slow = head;
    while (fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast)
        {
            struct ListNode* meet = slow;
            while(meet != head)
            {
                head = head->next;
                meet = meet->next;
            }
            return meet;
        }
    }
    return NULL;
}

💘不知不觉,【数据结构初阶】链表OJ以告一段落。通读全文的你肯定收获满满,让我们继续为数据结构学习共同奋进!!!

相关推荐
天地一流殇38 分钟前
SimBA算法实现过程
深度学习·算法·对抗攻击·黑盒
2501_924730611 小时前
智慧城管复杂人流场景下识别准确率↑32%:陌讯多模态感知引擎实战解析
大数据·人工智能·算法·计算机视觉·目标跟踪·视觉检测·边缘计算
weixin_307779131 小时前
C++实现MATLAB矩阵计算程序
开发语言·c++·算法·matlab·矩阵
学不动CV了1 小时前
FreeRTOS入门知识(初识RTOS任务调度)(三)
c语言·arm开发·stm32·单片机·物联网·算法·51单片机
Kingfar_12 小时前
智能移动终端导航APP用户体验研究案例分享
人工智能·算法·人机交互·ux·用户界面·用户体验
dlraba8022 小时前
机器学习-----SVM(支持向量机)算法简介
算法·机器学习·支持向量机
_poplar_2 小时前
09 【C++ 初阶】C/C++内存管理
c语言·开发语言·数据结构·c++·git·算法·stl
2501_924747113 小时前
驾驶场景玩手机识别准确率↑32%:陌讯动态特征融合算法实战解析
人工智能·算法·计算机视觉·智能手机
limitless_peter4 小时前
优先队列,链表优化
c++·算法·链表
fouryears_234175 小时前
参考线程池构建一个高性能、配置驱动的Docker容器池
docker·容器·oj