文章目录

  • 1.链表逆序
    • 1.1 题目描述
    • 1.2链表逆序的C++实现
  • 2.反转链表
    • 2.1 题目描述
    • 2.2 反转链表的C++实现
  • 3.求两个链表的交点
    • 3.1 题目描述
    • 3.2 C++实现——set
    • 3.3 C语言实现——链表长度实现
  • 4.链表求环
    • 4.1 题目描述
    • 4.2 C++实现
    • 4.3 C语言实现——快慢指针
  • 5.分隔链表
    • 5.1 题目描述
    • 5.2 C++实现
  • 6.复制带随机指针的链表
    • 6.1 题目描述
    • 6.2 C++实现
  • 7.排序链表的合并
    • 7.1 题目描述
    • 7.2 C++实现
  • 8.多个排序链表的合并
    • 8.1 题目描述
    • 8.2 C++实现
    • 8.3 C++实现——分治

1.链表逆序

1.1 题目描述

反转一个单链表。
示例:
\qquad输入: 1->2->3->4->5->NULL
\qquad输出: 5->4->3->2->1->NULL

1.2链表逆序的C++实现
#include<iostream>
using namespace std;struct ListNode {int val;ListNode* next;ListNode(int x):val(x),next(NULL){}
};
class Solution {public:ListNode* reverseList(ListNode* head) {ListNode* new_head = NULL;while (head) {ListNode* next;next = head->next;head->next = new_head;new_head = head;head = next;}return new_head;}
};

2.反转链表

2.1 题目描述

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。

2.2 反转链表的C++实现
struct ListNode {int val;ListNode* next;ListNode(int x):val(x),next(NULL){}
};
class Solution {public:ListNode* reverseBetween(ListNode* head, int left, int right) {int change_len = right-left+1;ListNode *pre_head=NULL;ListNode *result=head;while(head&&--left){pre_head=head;head=head->next;}ListNode *modify_list_tail=head;ListNode *new_head=NULL;while(head&&change_len){ListNode *next=head->next;head->next=new_head;new_head=head;head=next;change_len--;}modify_list_tail->next=head;if(pre_head){pre_head->next=new_head;}else{result=new_head;}return result;}
};

3.求两个链表的交点

3.1 题目描述

编写一个程序,找到两个单链表相交的起始节点。

3.2 C++实现——set
struct ListNode {int val;ListNode* next;ListNode(int x):val(x),next(NULL){}
};
class Solution {public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {set<ListNode*> Node_set;while(headA){Node_set.insert(headA);headA=headA->next;}while(headB){if(Node_set.find(headB)!=Node_set.end()){return headB;}headB=headB->next;}return NULL;}
};
3.3 C语言实现——链表长度实现

步骤1:计算headA和headB的长度
步骤2:将较长链表的指针移动到和较短链表指针对齐的地方
步骤3:两个链表同时移动,当两者指向同一个结点时,结点被找到

int get_list_legth(struct ListNode *head){int len=0;while(head){len++;head=head->next;}return len;}struct ListNode *forward_long_list(int long_len,int short_len,struct ListNode *head){int delta=long_len-short_len;while(head&&delta){head=head->next;delta--;}return head;}
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {int list_A_len=get_list_legth(headA);int list_B_len=get_list_legth(headB);if(list_A_len>list_B_len){headA=forward_long_list(list_A_len,list_B_len,headA);}if(list_B_len>list_A_len){headB=forward_long_list(list_B_len,list_A_len,headB);}while(headA&&headB){if(headA==headB){return headA;}headA=headA->next;headB=headB->next;}return NULL;
}

4.链表求环

4.1 题目描述

给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。

4.2 C++实现
class Solution {public:ListNode *detectCycle(ListNode *head) {set<ListNode *> node_list;while(head){if(node_list.find(head)!=node_list.end()){return head;}node_list.insert(head);head=head->next;}return NULL;}
};
4.3 C语言实现——快慢指针
struct ListNode *detectCycle(struct ListNode *head) {struct ListNode *fast=head;struct ListNode *slow=head;while(fast!=NULL){slow=slow->next;if(fast->next==NULL){return NULL;}fast=fast->next->next;if(fast==slow){struct ListNode *ptr=head;while(ptr){if(ptr==slow){return ptr;}ptr=ptr->next;slow=slow->next;}   }}return NULL;
}

5.分隔链表

5.1 题目描述

给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。

你应当 保留 两个分区中每个节点的初始相对位置。

5.2 C++实现
class Solution {public:ListNode* partition(ListNode* head, int x) {ListNode less_head(0);ListNode more_head(0);ListNode *less_ptr=&less_head;ListNode *more_ptr=&more_head;while(head){if(head->val<x){less_ptr->next=head;less_ptr=head;}else{more_ptr->next=head;more_ptr=head;}head=head->next;}less_ptr->next=more_head.next;more_ptr->next=NULL;return less_head.next;}
};

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

6.1 题目描述

题目描述

6.2 C++实现
#include<iostream>
using namespace std;struct ListNode {int val;ListNode* next;ListNode(int x):val(x),next(NULL){}
};
class Solution {public:Node* copyRandomList(Node* head) {map<Node *,int>  node_map;vector<Node *> node_vec;Node *ptr=head;int i=0;while(ptr){node_map[ptr]=i;node_vec.push_back(new Node(ptr->val));ptr=ptr->next;i++;}node_vec.push_back(0);ptr=head;i=0;while(ptr){node_vec[i]->next=node_vec[i+1];if(ptr->random){int id=node_map[ptr->random];node_vec[i]->random=node_vec[id];}ptr=ptr->next;i++;}return node_vec[0];}
};

7.排序链表的合并

7.1 题目描述

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

7.2 C++实现
class Solution {public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {ListNode temp_head(0);ListNode *pre=&temp_head;while(l1&&l2){if(l1->val<l2->val){pre->next=l1;l1=l1->next;}else{pre->next=l2;l2=l2->next;}pre=pre->next;}if(l1){pre->next=l1;}if(l2){pre->next=l2;}return temp_head.next;}
};

8.多个排序链表的合并

8.1 题目描述

给你一个链表数组,每个链表都已经按升序排列。

请你将所有链表合并到一个升序链表中,返回合并后的链表。

8.2 C++实现
bool cmp(ListNode *a,ListNode *b){return a->val<b->val;}
class Solution {public:ListNode* mergeKLists(vector<ListNode*>& lists) {vector<ListNode *> node_vec;int i;for(i=0;i<lists.size();i++){ListNode *head=lists[i];while(head){node_vec.push_back(head);head=head->next;}}if(node_vec.size()==0){return NULL;}sort(node_vec.begin(),node_vec.end(),cmp);for(i=1;i<node_vec.size();i++){node_vec[i-1]->next=node_vec[i];}node_vec[node_vec.size()-1]->next=NULL;return node_vec[0];}
};
8.3 C++实现——分治
class Solution {public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {ListNode temp_head(0);ListNode *pre=&temp_head;while(l1&&l2){if(l1->val<l2->val){pre->next=l1;l1=l1->next;}else{pre->next=l2;l2=l2->next;}pre=pre->next;}if(l1){pre->next=l1;}if(l2){pre->next=l2;}return temp_head.next;}ListNode* mergeKLists(vector<ListNode*>& lists) {if(lists.size()==0){return NULL;}if(lists.size()==1){return lists[0];}if(lists.size()==2){return mergeTwoLists(lists[0],lists[1]);}int mid=lists.size()/2;vector<ListNode *> sub1_lists;vector<ListNode *> sub2_lists;int i;for(i=0;i<mid;i++){sub1_lists.push_back(lists[i]);}for(i=mid;i<lists.size();i++){sub2_lists.push_back(lists[i]);}ListNode *l1=mergeKLists(sub1_lists);ListNode *l2=mergeKLists(sub2_lists);return mergeTwoLists(l1,l2);}
};

数据结构与算法——链表题目实现相关推荐

  1. JavaScript数据结构与算法——链表详解(下)

    在JavaScript数据结构与算法--链表详解(上)中,我们探讨了一下链表的定义.实现原理以及单链表的实现.接下来我们进一步了解一下链表的其他内容. 1.双向链表 双向链表实现原理图: 与单向链表不 ...

  2. JavaScript数据结构与算法——链表详解(上)

    注:与之前JavaScript数据结构与算法系列博客不同的是,从这篇开始,此系列博客采用es6语法编写,这样在学数据结构的同时还能对ECMAScript6有进一步的认识,如需先了解es6语法请浏览ht ...

  3. 数据结构与算法--链表实现以及应用

    数据结构与算法–链表实现以及应用 链表是面试时候使用最频繁的一种数据结构.链表的结构简单,他由指针将若干个节点链接成链状结构.链表的创建,插入,删除,查询操作都只有几行代码可以完成,代码量比较少,可以 ...

  4. 数据结构与算法-链表学习笔记

    数据结构与算法-链表学习笔记 链表的概念 链表是有序的列表. 链表是以节点的方式来存储,是链式存储,它在内存中并不是一定是连续的. 每个节点包含 data 域:存储数据, next 域:指向下一个节点 ...

  5. 数据结构与算法 -- 链表

    一.链表介绍 1.链表有地址不连续的结点序列,必须通过指针相互连接. 2.链表的分类: (1)单向线性链表 每个节点中除了存储数据结构内容以外,还需要保存指向下一个节点的指针,叫做后指针.最后一个节点 ...

  6. 数据结构与算法—链表常见面试题(持续更新)

    文章目录 一.链表环 1.判断链表是否有环 题目 方法1: 方法2: 二.反转链表 1.完全反转链表 题目: 方法1 方法2 2.反转部分链表 题目: 方法1 参考链接:https://blog.cs ...

  7. 数据结构与算法 —— 链表linked list(01)

    链表(维基百科) 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer).由于不必须按顺序存储, ...

  8. 数据结构与算法-链表篇

    链表与数组不同,是在物理空间中非连续的,依靠前一个节点记录下一个节点的地址进行连接的一种数据结构. 链表中每个节点存储的内容为当前节点和下个节点的指针. 链表一般分为三种 1.单向链表 2.双向链表 ...

  9. 常考数据结构和算法:链表中环的入口节点

    题目描述 对于一个给定的链表,返回环的入口节点,如果没有环,返回null. 步骤:  <1> 定义两个指针p1和p2,在初始化时都指向链表的头节点.  <2> 如果链表中的环有 ...

最新文章

  1. Unity 游戏开发技巧集锦之创建部分光滑部分粗糙的材质
  2. PMCAFF携手百度,干货从未如此精彩(现场快讯)
  3. android后台截屏实现(2)--screencap源码修改
  4. 关于假冒网站仿冒网易云信官网相关情况的声明
  5. codevs 1283 等差子序列
  6. 唯有自己变得强大_真正的自立,唯是让自己变得更加强大
  7. c语言编程被当作病毒,为什么这个微不足道的C程序被检测为病毒?
  8. 作业3:基于墨刀的短视频APP
  9. 海外同行首次大规模声援996.ICU,微软和GitHub员工签署联名信,一夜4700星
  10. 访谈《敏捷和精益项目集管理》的作者Johanna Rothman
  11. python迭代器和生成器_Python迭代器与生成器
  12. php怎么将农历转换成公历,php实现阳历阴历互转的方法
  13. 网站建设需遵循的六个步骤
  14. python中、常见的结构化数据不包括_数据分析的主要内容仍是结构化计算_数据分析师...
  15. csp m2 HRZ学英语
  16. 283页K8S实战指南,内容详实,代码齐全可复制!
  17. 扑克与投资哲学,活着最重要
  18. 微信支付一 :公众号支付1
  19. 计算机工程与科学北京,高强铝合金加速腐蚀方法的研究-计算机工程与科学-北京航空航天大学.PDF...
  20. css动画走马灯5秒,css3 transition 和 animation实现走马灯

热门文章

  1. LeetCode 1640. 能否连接形成数组(哈希)
  2. LeetCode 1473. 给房子涂色 III(DP)
  3. LeetCode 139. 单词拆分(DP)
  4. 平面设计中的网格系统pdf_平面设计基础知识
  5. 免费个人博客:使用hexo+github搭建详细教程
  6. PAT乙类1009 说反话 (20 分)
  7. 做CV和做NLP,是否都有光明的未来?
  8. 谈谈怎样提高炼丹手速
  9. 下载 | 李宏毅:1 天搞懂深度学习,我总结了 300 页 PPT
  10. 消息中间件系列(四):消息队列MQ的特点、选型、及应用场景详解