328. Odd Even Linked List

Total Accepted: 9271 Total
Submissions: 24497 Difficulty: Easy

Given a singly linked list, group all odd nodes together followed by the even nodes.

Please note here we are talking about the node number and not the value in the nodes.

You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.

Example:

Given 1->2->3->4->5->NULL,

return 1->3->5->2->4->NULL.

Note:

The relative order inside both the even and odd groups should remain as it was in the input. 

The first node is considered odd, the second node even and so on ...

分析:

简单模拟思想。

申请两个指针,一个总是指向当前将要建立连接的奇数节点,一个偶数...最后联立奇数链表和偶数链表就可以。

显然,先建立奇数指针。再偶数指针....

建立奇数指针时,看偶数指针的下一个位置是否存在。存在就建立连接,

并将奇数指针移动当当前奇数节点!

假设不存在就完毕偶数链和奇数链的终于联立。

偶数指针同理。

class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(!head || head->next==NULL || head->next->next==NULL)
return head;
ListNode* oddNode=head;//奇数
ListNode* evenNode=head->next;//偶数
ListNode* evenhead=head->next;//偶数头
while(true)
{
if(evenNode->next!=NULL)//假设存在就建立连接
{
oddNode->next=evenNode->next;
oddNode=evenNode->next;//偶数尾巴
}else
{
oddNode->next=evenhead;
evenNode->next=NULL;
break;//建立偶数链表与奇数链表的连接退出循环
} if(oddNode->next!=NULL)
{
evenNode->next=oddNode->next;
evenNode=oddNode->next;
}else
{
oddNode->next=evenhead;
evenNode->next=NULL;
break;//建立......连接退出循环
}
}
return head;
}
};

简化代码:

class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(!head || head->next==NULL || head->next->next==NULL)
return head;
ListNode* oddNode=head;//奇数
ListNode* evenNode=head->next;//偶数
ListNode* evenhead=head->next;//偶数头
while(evenNode!=NULL && evenNode->next !=NULL)//无论链表有奇数个还是偶数个节点都以偶数指针作为截止条件
{
oddNode->next=evenNode->next;
oddNode=evenNode->next;//偶数尾巴
evenNode->next=oddNode->next;
evenNode=oddNode->next;
}
oddNode->next=evenhead;
return head;
}
};

注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50611707

原作者博客:http://blog.csdn.net/ebowtang

本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895

<LeetCode OJ> 328. Odd Even Linked List的更多相关文章

  1. [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)

    每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...

  2. 【一天一道LeetCode】#328 Odd Even Linked List

    一天一道LeetCode系列 (一)题目 Given a singly linked list, group all odd nodes together followed by the even n ...

  3. 【LeetCode】328. Odd Even Linked List 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. LeetCode 328. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  5. Java [Leetcode 328]Odd Even Linked List

    题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...

  6. LeetCode 328. Odd Even Linked List C#

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  7. (链表) leetcode 328. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  8. 【Leetcode】 328. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  9. 【leetcode】328. Odd Even Linked List

    题目如下: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...

随机推荐

  1. MySQL 的七种 join

    建表 在这里呢我们先来建立两张有外键关联的张表. CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`tbl_dept`( `id` ...

  2. MATLAB的cftool工具箱简介

    下面,通过一个例子说明cftool可视化界面工具箱的用法. 例如,已知 x = [0 0.2 0.50.8 0.9 1.3 1.4 1.9 2.1 2.2 2.5 2.6 2.9 3.0]; y = ...

  3. Android Studio查看类中所有方法和属性

    ctrl+f3效果: alt+7效果: 注意区别:虽然所有方法都有,但是顺序自己一看效果便知.一个是根据类中的顺序,另一个是根据a-z的开头字母顺序. 百度查了一下快捷键是ctrl+f12.但是自己试 ...

  4. Jest+Enzyme React js/typescript测试环境配置案例

    本文案例github:https://github.com/axel10/react-jest-typescript-demo 配置jest的react测试环境时我们可以参考官方的配置教程: http ...

  5. xunsearch如何按照ID排序

    你ini再建一个字段id_tmp 类型type=numeric 重建索引的时候 数据源 加一个主键id的别名 id, id as id_tmp 排序的时候按照id_tmp排序

  6. 原生js编写设为首页兼容ie、火狐和谷歌

    // JavaScript Document // 加入收藏 <a onclick="AddFavorite(window.location,document.title)" ...

  7. dart Stream

  8. Proxmox VE

    Proxmox虚拟化环境是基于QEMU/KVM和LXC的开源服务器虚拟化管理解决方案.我们可以使用集成的易于使用的WEB界面或通过CLI管理虚拟机,容器,高可用集群,存储和网络. Proxmox VE ...

  9. [UOJ206]Gap

    子任务$1$:直接找到最大值后从两边开始找,一步一步从左右到中间确定所有数,调用次数是$\left\lceil\dfrac n2\right\rceil$ 子任务$2$:先找到最大值$mx$和最小值$ ...

  10. [AGC012D]Colorful Balls

    题意:有$N$个球,有颜色$c_i$,重量$w_i$,若($c_a=c_b$且$w_a+w_b\leq X$)或($c_a\ne c_b$且$w_a+w_b\leq Y$),可以交换$a,b$,求总共 ...