试剂产品商城网站建设,网站制作属于什么专业,wordpress 直播主题,有没有专业做特产的网站目录
1、移除链表元素
2、翻转链表
3、合并两个有序链表
4、获取链表的中间结点
5、环形链表解决约瑟夫问题
6、分割链表 1、移除链表元素
203. 移除链表元素 - 力扣#xff08;LeetCode#xff09;
typedef struct ListNode LSNode;
struct ListNode* remove…目录
1、移除链表元素
2、翻转链表
3、合并两个有序链表
4、获取链表的中间结点
5、环形链表解决约瑟夫问题
6、分割链表 1、移除链表元素
203. 移除链表元素 - 力扣LeetCode
typedef struct ListNode LSNode;
struct ListNode* removeElements(struct ListNode* head, int val){LSNode* newHead,*newTail;//令头结点和尾结点都置为空newHead newTail NULL;//令新指针pcur指向原链表的头结点head遍历原链表LSNode* pcur head;while(pcur){//当不满足.valval时开始向新建的空链表中插入if(pcur-val ! val){//1、如果新建的链表为空插入的新节点就是链表的头结点和尾结点if(newHead NULL){newHead newTail pcur;}//2、如果新建的链表不为空直接尾插让新插进来的结点作为新的尾结点else{newTail-next pcur;newTail newTail-next;//令newTail移位}}//当满足pcru-val val直接跳过进行下一个读取即可pcur pcur-next;}//当pcur指向存储整数6的结点时pcur满足pcur.val val不会进入if直接执行pcur pcur-next此时pcur NULL//pcur为NULL跳出while循环如果此时直接返回newHead那么新链表的newTail-next指向的位置仍是旧链表存储数据6//的结点所以此时需要再判断newTail是否为空如果不为空则让它最后指向的方向置为空最后再返回头结点if(newTail)newTail-next NULL;return newHead;
} 2、翻转链表 206. 反转链表 - 力扣LeetCode
typedef struct ListNode LSNode;
struct ListNode* reverseList(struct ListNode* head)
{//如果传入的链表为空的时候直接返回NULLif(head NULL){return NULL;}LSNode* n1,*n2,*n3;n1 NULL; n2 head;n3 head-next;while(n2){n2-next n1;n1 n2;//当n3为空时已经将n3的值交给n2n2 n3;//当n3所处的位置不为空时才能接着移动n3否则结束一次while循环if(n3)n3 n3-next;}//此时n1为链表的头return n1;
} 3、合并两个有序链表 21. 合并两个有序链表 - 力扣LeetCode
typedef struct ListNode LSNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{//当传入的两个链表其中有一个为空那么返回另一个链表即可if(list1 NULL){return list2;}if(list2 NULL){return list1;}//当两个链表都不为空时遍历链表LSNode* cur1 list1;LSNode* cur2 list2;//创建新的空链表--带头结点单向不循环链表后续进行尾插等情况就不需要考虑头结点是否为空的情况减少重复代码LSNode* newHead,*newTail;//malloc创建一个内存空间该空间不含有效数据刚好用于存放该链表的头结点头结点的有空间但是不存储有效数据newHead newTail (LSNode*)malloc(sizeof(LSNode));//当两个结点有一个走到空就不能进行比较了while(cur1 cur2){//把值小的结点尾插到新的链表if(cur1-val cur2-val){newTail-next cur1;newTail newTail-next;cur1 cur1-next;}//当cur2-val cur1-val时else{newTail-next cur2;newTail newTail-next;cur2 cur2-next;}}
if(cur1)newTail-next cur1;
if(cur2)newTail-next cur2;
return newHead-next;
} 4、获取链表的中间结点
876. 链表的中间结点 - 力扣LeetCode
typedef struct ListNode LSNode;
struct ListNode* middleNode(struct ListNode* head)
{ if(head NULL)return NULL;//快慢指针LSNode* slow,*fast;slow fast head;//只要fast和fast-next有一个为空则停止循环//因为我们也不知道链表的结点数是奇数还是偶数while(fast fast-next)//注意二者判断顺序不能交换因为如果链表结点数为偶数时最后一次循环 //fast指向的位置刚好空下次循环前判断时由于fast以及指向空了更别提fast-next了//虽然此时slow指向了我们想要的位置但是由于fast-next本身就不合理程序就会报错//当然如果是奇数个就可以交换{slow slow-next;fast fast-next-next;}//当循环结束时slow必定指向我们要找的链表中间结点return slow;
} 5、环形链表解决约瑟夫问题
环形链表的约瑟夫问题_牛客题霸_牛客网 (nowcoder.com)
#include stdio.h
#include stdlib.h
typedef struct ListNode ListNode;//申请链表结点函数同时为结点中添加数据x
ListNode* ListByNode(int x)
{ListNode* node (ListNode*)malloc(sizeof(ListNode));if(node NULL){perror(malloc fail!);exit(1);}node-val x;node-next NULL;return node;
}//创建带环链表
ListNode* CreateList(int n)
{ListNode* phead ListByNode(1);ListNode* pTail phead;for(int i 2;in;i){ListNode* node ListByNode(i);pTail-next node;pTail pTail-next;}//以上只是在创建单链表想要让链表成环需要将尾结点和头结点相连pTail-next phead;//这里直接返回尾结点因为有尾结点就能直接找到头结点返回头结点的话还需要遍历链表才能找到尾结点return pTail;
}//实现函数
int ysf(int n, int m ) {//创建不带头单向循环链表ListNode* prev CreateList(n);//进行游戏逻辑实现ListNode* cur prev-next;//就是头结点int count 1;while (cur-next ! cur) {if(count m){//删除结点prev-next cur-next;free(cur);cur prev-next;count 1;//人死后记得让下一个人从1开始报数count重置为初始值1}else {//继续向下报数prev cur;cur cur-next;count;}}//此时链表中只剩下一个结点返回该结点中的数return cur-val;
} 6、分割链表
面试题 02.04. 分割链表 - 力扣LeetCode
typedef struct ListNode ListNode;
struct ListNode* partition(struct ListNode* head, int x)
{if(head NULL)return head;//创建带头的大小链表ListNode* lessHead,*lessTail;ListNode* greatHead,*greatTail;//创建大小链表的哨兵位lessHead lessTail (ListNode*)malloc(sizeof(ListNode));greatHead greatTail (ListNode*)malloc(sizeof(ListNode));//遍历原链表将结点放到大小链表中ListNode* cur head;//当cur读取原链表后循环结束while(cur){ //放入小链表if(cur-val x){lessTail-next cur;lessTail lessTail-next;}//放入大链表else{greatTail-next cur;greatTail greatTail-next;}cur cur-next; //cur向后走}//原链表循环结束此时greatTail后指向的内容并未被置空所以要判断if(greatTail)greatTail-next NULL;//小链表的尾和大链表的哨兵位的下一个结点连接起来lessTail-next greatHead-next;return lessHead-next;
} ~over~