143. Reorder List

https://www.cnblogs.com/grandyang/p/4254860.html

先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接

class Solution {
public:
void reorderList(ListNode* head) {
if(head == NULL)
return;
ListNode* p1 = head;
ListNode* p2 = head;
while(p2->next != NULL && p2->next->next != NULL){
p1 = p1->next;
p2 = p2->next->next;
}
p2 = p1->next;
p1->next = NULL;
ListNode* pre = NULL;
while(p2 != NULL){
ListNode* tmp = p2->next;
p2->next = pre;
pre = p2;
p2 = tmp;
}
p1 = head;
p2 = pre;
while(p2 != NULL){
ListNode* tmp1 = p1->next;
ListNode* tmp2 = p2->next;
p1->next = p2;
p2->next = tmp1;
p1 = tmp1;
p2 = tmp2;
}
return;
}
};

86. Partition List

这个题和143有点相似,都是用两个指针,分别表示前面满足条件的最后一个,后面满足条件的最后一个。

但143的p2是只管当前指针,但86题p2是当前指针的前一个指针,因为86题需要判断是否满足小于x的条件,但143则不需要。

主循环那部分,只适合下一个指针大于x的情况,所以先通过一个循环找到第一个大于的x的前一个指针。

注意:要申请一个dummy指针保存最终的链表的返回,head指针也可能被排序排乱。

比如下面这种情况:

Input:
[2,1]
2
Output:
[2]
Expected:
[1,2]

class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if(head == NULL)
return NULL;
ListNode* dummy = new ListNode(-);
dummy->next = head;
ListNode* pre = dummy;
while(pre->next && pre->next->val < x)
pre = pre->next;
ListNode* cur = pre;
while(cur->next){
if(cur->next->val >= x)
cur = cur->next;
else{
ListNode* tmp = cur->next;
cur->next = tmp->next;
tmp->next = pre->next;
pre->next = tmp;
pre = pre->next;
}
}
return dummy->next;
}
};

leetcode 143. Reorder List 、86. Partition List的更多相关文章

  1. leetcode 143. Reorder List ----- java

    Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do thi ...

  2. Java for LeetCode 143 Reorder List

    Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...

  3. Leetcode#143 Reorder List

    原题地址 先把链表分割成前后两半,然后交叉融合 实践证明,凡是链表相关的题目,都应该当成工程类题目做,局部变量.功能函数什么的随便整,代码长了没关系,关键是清楚,不容易出错. 代码: ListNode ...

  4. Leetcode 143. Reorder List(Medium)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  5. [leetcode]143. Reorder List重排链表

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...

  6. 【LeetCode】143. Reorder List 解题报告(Python)

    [LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  7. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  8. 143. Reorder List - LeetCode

    Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...

  9. LeetCode:分割链表【86】

    LeetCode:分割链表[86] 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例 ...

随机推荐

  1. 解决npm安装时出现run `npm audit fix` to fix them, or `npm audit` for details 的问题

    npm audit fix npm audit fix --force npm audit 按照顺序一一运行亲测完全可用如果还是不行的话,可以把node_modules和package-lock.js ...

  2. 异常-No suppression parameter found for notification

    1 详细异常 Command Start is not currently available for execution. 关闭 kafka gateway 无法启动 java.lang.NullP ...

  3. Linux命令——source

    参考:What does 'source' do? 前言 当我们修改了/etc/profile文件,并想让它立刻生效,而不用重新登录,就可以使用source命令,如source /etc/profil ...

  4. Song Form

    First of all, song form is an indepentent concept from the boxes, boxes simply describe the way the ...

  5. (2) openstack--keystone

    yun1 OpenStack packages yum install python-openstackclient -y yum install openstack-selinux SQL data ...

  6. AcWing P164 可达性统计 题解

    Analysis 这道题我一开始想到的是传递闭包,但是时间复杂度是n³,也开不下30000*30000的数组,所以我想到了拓扑+状态压缩(bitset),从后往前找,把能到达的点能到哪里用位运算赋到上 ...

  7. Oracle 10g和11g中的自动统计任务

    1)  先来看下oracle 10g中的自动统计任务的问题. 从Oracle Database 10g开始,Oracle在建库后就默认创建了一个名为GATHER_STATS_JOB的定时任务,用于自动 ...

  8. linux系列(十六):which命令

    1.命令格式: which 可执行文件名称 2.命令功能: which指令会在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果. 3.命令参数: -n 指定文件名长度,指定的长 ...

  9. requests记录

    http://docs.python-requests.org/zh_CN/latest/user/quickstart.html r = requests.get('http://httpbin.o ...

  10. 配置了ssh免密登录还是提示权限不足怎么解决

    通过 管理终端 进入系统.通过 cat 等指令查看 /etc/ssh/sshd_config 中是否包含类似如下配置: AllowUsers root test DenyUsers test Deny ...