当前位置: 首页 > news >正文

中卫网站推广公司网站网络排名优化方法

中卫网站推广公司,网站网络排名优化方法,学院网站建设的特色,祥符网站建设链表OJ 题目一:移除链表元素题目二:反转链表题目三:链表的中间节点题目四:链表中倒数第k个结点题目五:合并两个有序链表题目六:链表分割题目七:链表的回文结构题目八:相交链表题目九…

链表OJ

    • 题目一:移除链表元素
    • 题目二:反转链表
    • 题目三:链表的中间节点
    • 题目四:链表中倒数第k个结点
    • 题目五:合并两个有序链表
    • 题目六:链表分割
    • 题目七:链表的回文结构
    • 题目八:相交链表
    • 题目九:环形链表
    • 题目十:环形链表II

题目一:移除链表元素

OJ
在这里插入图片描述
方案一:

题目解析:
在这里插入图片描述

代码演示:
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;
}

方案二:

题目解析:把原链表遍历一遍,插入新链表
在这里插入图片描述

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
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

题目解析:
在这里插入图片描述

代码演示:
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
在这里插入图片描述

题目解析:
在这里插入图片描述

代码演示:
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
在这里插入图片描述

题目解析:
在这里插入图片描述

代码演示:
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
在这里插入图片描述

题目解析:
在这里插入图片描述

代码演示:
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
在这里插入图片描述

题目解析:
在这里插入图片描述

代码演示:
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
在这里插入图片描述

题目解析:
在这里插入图片描述

代码演示:
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
在这里插入图片描述

题目解析:
定义快慢指针,使快指针先走与慢指针同步。然后同时走看是否相交
在这里插入图片描述

代码演示:
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

在这里插入图片描述

题目解析:
在这里插入图片描述

代码演示:
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
在这里插入图片描述

题目解析:
在这里插入图片描述

代码演示:
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以告一段落。通读全文的你肯定收获满满,让我们继续为数据结构学习共同奋进!!!

http://www.hrbkazy.com/news/37070.html

相关文章:

  • 哪个网站系统做的好外链群发平台
  • 网站开发的初级技术员seo推广一个月见效
  • 网站设计制作排名百度网盟推广官方网站
  • 随州学做网站seo网站推广的主要目的不包括
  • 长沙优化网站技术厂家网络推广怎么推广
  • 2021年江东seo做关键词优化
  • 重庆网站定制哪家好东莞seo外包
  • 事业单位网站登录模板seo排名助手
  • 女女做那个动漫视频网站seo快速优化软件
  • 利用菜刀软件xise做网站劫持舆情分析系统
  • 外贸网站建设如何做深圳百度seo整站
  • 找别人做网站注意问题网站策划书怎么写
  • 公司网站设计seo自动优化软件下载
  • 廊坊建站模板系统google adwords关键词工具
  • 上传引用图片 网站新闻头条今日要闻军事
  • 临汾网站建设电话长沙专业seo优化公司
  • 做地方分类信息网站需要什么资质吗无锡网络推广外包
  • windows2008 iis 网站配置常见的推广方式有哪些
  • 卫计局本年度网站建设工作总结软文
  • 湛江网站设计哪家好永久免费个人网站申请注册
  • 医院做网站备案需要哪些资料创建网站的软件
  • 天河wap网站建设公司详细描述如何进行搜索引擎的优化
  • 长沙正规竞价优化推荐杭州seo搜索引擎优化
  • 柳州最好的网站推广公司网站收录查询入口
  • 企业网站建设的策略佛山网站建设技术托管
  • 上海网站设计排名深圳百度seo哪家好
  • 怎么做一个聊天软件滕州seo
  • 网站空间注册百度推广登录首页网址
  • 帮做毕设的网站线上推广怎么做
  • 做行程规划的旅行网站游戏推广怎么快速拉人