网站续费贵是重新做个好还是续费,mvc做门户网站,大连建设工程网站,系统总裁文章目录 一、206.反转链表二、92.反转链表 ||三、25. K 个一组翻转链表 一、206.反转链表 class Solution {
public://使用头插//三个指针也可以ListNode* reverseList(ListNode* head) {if(headnullptr)return nullptr;ListNode* curhead;ListNode* newheadnew ListNode(0);L… 文章目录 一、206.反转链表二、92.反转链表 ||三、25. K 个一组翻转链表 一、206.反转链表 class Solution {
public://使用头插//三个指针也可以ListNode* reverseList(ListNode* head) {if(headnullptr)return nullptr;ListNode* curhead;ListNode* newheadnew ListNode(0);ListNode* prenewhead;while(cur){ListNode* nextcur-next;cur-nextpre-next;pre-nextcur;curnext;}curnewhead-next;delete newhead;return cur;}
};二、92.反转链表 || 给你单链表的头指针 head 和两个整数 left 和 right 其中 left right 。请你反转从位置 left 到位置 right 的链表节点返回 反转后的链表 /更简洁
class Solution {
public:ListNode *reverseBetween(ListNode *head, int left, int right) {ListNode *dummy new ListNode(0, head), *p0 dummy;for (int i 0; i left - 1; i)p0 p0-next;ListNode *pre nullptr, *cur p0-next;for (int i 0; i right - left 1; i) {ListNode *nxt cur-next;cur-next pre; // 每次循环只修改一个 nextpre cur;cur nxt;}p0-next-next cur;p0-next pre;return dummy-next;}
};// class Solution {
// public://使用头插使用哨兵节点left可能为一
// ListNode* reverseBetween(ListNode* head, int left, int right) {
// if(leftright)
// {
// return head;
// }
// int sign1;
// ListNode* curhead;
// ListNode* newheadnew ListNode(0);
// ListNode* tmpnewhead;
// ListNode* firstinsertnullptr;
// while(cur)
// {
// ListNode* nextcur-next;
// if(signleft)
// {
// firstinsertcur;
// }
// if(signleft signright)
// {
// cur-nexttmp-next;
// tmp-nextcur;
// curnext;
// if(signright)
// {
// tmpfirstinsert;
// if(curnullptr)
// {
// firstinsert-nextnullptr;
// }
// }
// }
// else
// {
// tmp-nextcur;
// tmpcur;
// curnext;
// }
// sign;
// }
// curnewhead-next;
// delete newhead;
// return cur;
// }
// };三、25. K 个一组翻转链表 给你链表的头节点 head 每 k 个节点一组进行翻转请你返回修改后的链表。 k 是一个正整数它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍那么请将最后剩余的节点保持原有顺序。 你不能只是单纯的改变节点内部的值而是需要实际进行节点交换。 class Solution {
public://思路是进行头插ListNode *reverseKGroup(ListNode *head, int k) {//先遍历链表看需要反转几次int n0;ListNode* curhead;while(cur){curcur-next;n;}nn/k;//这就是要反转几次的结果curhead;ListNode* newheadnew ListNode(0);ListNode* prenewhead;for(int i0;in;i){ListNode* tmpcur;for(int i0;ik;i){ListNode* nextcur-next;cur-nextpre-next;pre-nextcur;curnext;}pretmp;}//处理不需要反转的pre-nextcur;curnewhead-next;delete newhead;return cur;}
};