Day13反转链表两两交换链表中的节点删除链表的倒数第N个节点链表相交环形链表II

news/2025/2/19 8:58:59/

反转链表

struct ListNode* reverseList(struct ListNode* head){struct ListNode* pre=NULL;while(head){struct ListNode* temp=head->next;head->next=pre;pre=head;head=temp;}return pre;
}

两两交换链表中的节点

struct ListNode* swapPairs(struct ListNode* head){struct ListNode* pre;struct ListNode* cur;struct ListNode* temp;struct ListNode* pHead=(struct ListNode*)malloc(sizeof(struct ListNode));struct ListNode* shead=pHead;pHead->next=head;pre=cur=head;while( pre && cur->next   ){cur=cur->next;temp=cur->next;shead->next=cur;cur->next=pre;pre->next=temp;shead=pre;pre=temp;cur=pre;}return pHead->next;
}

删除链表的倒数第N个节点

struct ListNode* removeNthFromEnd(struct ListNode* head, int n){struct ListNode* sHead;sHead=(struct ListNode*)malloc(sizeof(struct ListNode));sHead->val=0;sHead->next=head;struct ListNode* slow=sHead;struct ListNode* fast=head;int i=n;while( fast ){if(n){fast=fast->next;n--;}else{fast=fast->next;slow=slow->next;}}slow->next=slow->next->next;return sHead->next;}

链表相交

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {if(headA==NULL || headB==NULL ) return NULL;int lenA=0,lenB=0;int gap;struct ListNode* s;struct ListNode* l;s=headA;while(s){s=s->next;lenA++;}s=headB;while(s){s=s->next;lenB++;}if(lenA>lenB) {gap=lenA-lenB;l=headA;s=headB;}else{gap=lenB-lenA;l=headB;s=headA;}while(gap--) l=l->next;while(l){if(l==s) return s;l=l->next;s=s->next;}return NULL;
}

环形链表II

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

http://www.ppmy.cn/news/1196804.html

相关文章

ConnectionError: HTTPSConnectionPool

ConnectionError: HTTPSConnectionPool(host‘zbbfxstatic.figtingdream.com’, port443): Max retries exceeded with url: /api/cache (Caused by NewConnectionError(‘<urllib3.connection.HTTPSConnection object at 0x00000249795AD9A0>: Failed to establish a ne…

21.合并两个有序链表

#include <iostream>struct ListNode {int val;ListNode* next;ListNode(int x) : val(x), next(nullptr) {} };class Solution { public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {ListNode dummy ListNode(-1); // 创建一个虚拟节点作为头节点ListNode* …

C#知识总结 基础篇(下)

目录 5类和继承 5.1类继承 5.2访问继承的成员 5.3屏蔽基类的成员 5.4访问基类的成员 5.5虚方法与覆写方法 5.6构造函数的执行顺序 5.7成员访问修饰符 5.8抽象类 5.9密封类与静态类 6.表达式与运算符 6.1运算符和重载 7.结构 7.1结构体的感念。 7.2结构构造函数与…

Rocky 安装jdk17

1&#xff09;检测jdk是否安装&#xff1a; #运行 java -version如果提示安装&#xff0c;则输入N&#xff0c;跳过 2&#xff09;检测cpu 类型 若未安装查看linux处理器架构&#xff1a; #运行 hostnamectl #或运行 arch 3&#xff09;去官网下载相应的编译版本的Jdk Or…

成人编程先学什么?成人编程一般要学几年

成人编程先学什么&#xff1f;成人编程一般要学几年 给大家分享一款中文编程工具&#xff0c;零基础轻松学编程&#xff0c;不需英语基础&#xff0c;编程工具可下载。 这款工具不但可以连接部分硬件&#xff0c;而且可以开发大型的软件&#xff0c;向如图这个实例就是用这个…

【Java】汉诺塔

汉诺塔 汉诺塔&#xff08;Tower of Hanoi&#xff09;&#xff08;河内塔&#xff09;&#xff1a;把圆盘从下面开始按大小顺序重新摆放到另一根柱子上&#xff0c;并且小圆盘上不能放大圆盘&#xff0c;在三根柱子之间一次只能移动一个圆盘。 汉诺塔规则 disk表示圆盘数一次只…

CodeWhisperer 的安装及体验

CodeWhisperer 是亚马逊出品的一款基于机器学习的通用代码生成器&#xff0c;可实时提供代码建议。类似 Cursor 和 Github Copilot 编码工具。 官网&#xff1a;aws.amazon.com/cn/codewhis… 在编写代码时&#xff0c;它会自动根据您现有的代码和注释生成建议。从单行代码建…

抵押贷款巨头 Mr. Cooper 遭受网络攻击,影响 IT 系统

导语 近日&#xff0c;美国抵押贷款巨头 Mr. Cooper 遭受了一次网络攻击&#xff0c;导致该公司的 IT 系统受到影响。这一事件引起了广泛的关注&#xff0c;使得 Mr. Cooper 的在线支付平台无法正常运行。本文将为大家详细介绍这次网络攻击事件的具体情况及其对用户和公司造成的…