网站建设所需,iis做网站上传速度慢,怎么做网站网站不被发现,小说网站做公众号好还是网站好目录
一、回文链表
二、 重排链表
三、旋转链表 一、回文链表
给你一个单链表的头节点 head #xff0c;请你判断该链表是否为回文链表。如果是#xff0c;返回 true #xff1b;否则#xff0c;返回 false 。
示例 1#xff1a; 输入#xff1a;head [1,2,2,1] 输…目录
一、回文链表
二、 重排链表
三、旋转链表 一、回文链表
给你一个单链表的头节点 head 请你判断该链表是否为回文链表。如果是返回 true 否则返回 false 。
示例 1 输入head [1,2,2,1] 输出true 示例 2 输入head [1,2] 输出false 提示 链表中节点数目在范围[1, 10^5] 内 0 Node.val 9
进阶你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题
代码实现
struct ListNode* middleNode(struct ListNode* head)
{struct ListNode* slow head;struct ListNode* fast head;while (fast ! NULL fast-next ! NULL){slow slow-next;fast fast-next-next;}return slow;
}
struct ListNode* reverseList(struct ListNode* head)
{struct ListNode* pre NULL;struct ListNode* cur head;while (cur ! NULL){struct ListNode* after cur-next;cur-next pre;pre cur;cur after;}return pre;
}
bool isPalindrome(struct ListNode* head)
{// 1. 找到中间结点如果有两个中间结点则找到第二个中间结点struct ListNode* mid middleNode(head);// 2. 从中间结点开始对后半段进行反转struct ListNode* rightHead reverseList(mid);// 3. 进行前半段和后半段的比较struct ListNode* leftCur head;struct ListNode* rightCur rightHead;while (rightCur ! NULL){if (leftCur-val ! rightCur-val){return false;}leftCur leftCur-next;rightCur rightCur-next;}return true;
} 回文结构是一个生物学名词。双链 DNA 中含有的两个结构相同、方向相反的序列称为回文结构每条单链以任一方向阅读时都与另一条链以相反方向阅读时的序列是一致的。 palindrome n. 回文结构回文序列 牛津a word or phrase that reads the same backwards as forwards, for example madam. 二、 重排链表
给定一个单链表 L 的头节点 head 单链表 L 表示为 L0 → L1 → … → Ln - 1 → Ln 请将其重新排列后变为 L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → … 不能只是单纯的改变节点内部的值而是需要实际的进行节点交换。
示例 1 输入head [1,2,3,4] 输出[1,4,2,3] 示例 2 输入head [1,2,3,4,5] 输出[1,5,2,4,3] 提示 链表的长度范围为 [1, 5 * 10^4] 1 node.val 1000
代码实现
struct ListNode* middleNode(struct ListNode* head)
{struct ListNode* slow head;struct ListNode* fast head;while (fast fast-next){slow slow-next;fast fast-next-next;}return slow;
}
struct ListNode* reverseList(struct ListNode* head)
{struct ListNode* pre NULL;struct ListNode* cur head;while (cur ! NULL){struct ListNode* after cur-next;cur-next pre;pre cur;cur after;}return pre;
}
void reorderList(struct ListNode* head)
{// 1. 找到中间结点如果有两个中间结点则找到第二个中间结点struct ListNode* mid middleNode(head);// 2. 从中间结点开始对后半段进行反转struct ListNode* rightHead reverseList(mid);// 3. 重排链表struct ListNode* leftCur head;struct ListNode* rightCur rightHead;while (rightCur-next ! NULL){struct ListNode* leftAfter leftCur-next;struct ListNode* rightAfter rightCur-next;leftCur-next rightCur;rightCur-next leftAfter;leftCur leftAfter;rightCur rightAfter;}
}
图解示例二 三、旋转链表
给你一个链表的头节点 head 旋转链表将链表每个节点向右移动 k 个位置。
示例 1 输入head [1,2,3,4,5], k 2 输出[4,5,1,2,3] 示例 2 输入head [0,1,2], k 4 输出[2,0,1] 提示 链表中节点的数目在范围 [0, 500] 内 -100 Node.val 100 0 k 2 * 109
代码实现
struct ListNode* rotateRight(struct ListNode* head, int k)
{if (head NULL){return NULL;}// 1. 找到链表的尾结点并计算它的长度struct ListNode* tail head;int len 1;while (tail-next ! NULL){len;tail tail-next;}// 2. 找到倒数第 (k % len 1) 个结点k % len;if (k 0){return head;}struct ListNode* pre head;for (int i 0; i len - k - 1; i){pre pre-next;}// 3. 旋转链表tail-next head; // (1)head pre-next; // (2)pre-next NULL; // (3)return head;
} 文章转载自: http://www.morning.zwwhq.cn.gov.cn.zwwhq.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.rnrwq.cn.gov.cn.rnrwq.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.qnkqk.cn.gov.cn.qnkqk.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.btrfm.cn.gov.cn.btrfm.cn http://www.morning.dtmjn.cn.gov.cn.dtmjn.cn http://www.morning.kqcqr.cn.gov.cn.kqcqr.cn http://www.morning.glnmm.cn.gov.cn.glnmm.cn http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.gqbks.cn.gov.cn.gqbks.cn http://www.morning.mlgsc.com.gov.cn.mlgsc.com http://www.morning.chzbq.cn.gov.cn.chzbq.cn http://www.morning.gbsfs.com.gov.cn.gbsfs.com http://www.morning.lhwlp.cn.gov.cn.lhwlp.cn http://www.morning.kwqwp.cn.gov.cn.kwqwp.cn http://www.morning.pwsnr.cn.gov.cn.pwsnr.cn http://www.morning.rpgdd.cn.gov.cn.rpgdd.cn http://www.morning.smqjl.cn.gov.cn.smqjl.cn http://www.morning.ysckr.cn.gov.cn.ysckr.cn http://www.morning.zdhxm.com.gov.cn.zdhxm.com http://www.morning.nkhdt.cn.gov.cn.nkhdt.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.cflxx.cn.gov.cn.cflxx.cn http://www.morning.ghfrb.cn.gov.cn.ghfrb.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.sjftk.cn.gov.cn.sjftk.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.zyrcf.cn.gov.cn.zyrcf.cn http://www.morning.kkzwn.cn.gov.cn.kkzwn.cn http://www.morning.tqbyw.cn.gov.cn.tqbyw.cn http://www.morning.qfths.cn.gov.cn.qfths.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.kwjyt.cn.gov.cn.kwjyt.cn http://www.morning.nzfqw.cn.gov.cn.nzfqw.cn http://www.morning.fbbpj.cn.gov.cn.fbbpj.cn http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.tfrlj.cn.gov.cn.tfrlj.cn http://www.morning.hlmkx.cn.gov.cn.hlmkx.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.kqpsj.cn.gov.cn.kqpsj.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.rrrrsr.com.gov.cn.rrrrsr.com http://www.morning.bnfjh.cn.gov.cn.bnfjh.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn http://www.morning.pzcqz.cn.gov.cn.pzcqz.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.kxypt.cn.gov.cn.kxypt.cn http://www.morning.gkjnz.cn.gov.cn.gkjnz.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.ptzbg.cn.gov.cn.ptzbg.cn http://www.morning.pctsq.cn.gov.cn.pctsq.cn http://www.morning.kgxrq.cn.gov.cn.kgxrq.cn http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.zdzgf.cn.gov.cn.zdzgf.cn http://www.morning.dkcpt.cn.gov.cn.dkcpt.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.vjdofuj.cn.gov.cn.vjdofuj.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn