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 ...

Tips:本题给定一个链表,要求将所有偶数位置的结点聚集在一起,连接在奇数位置的结点之后(调整数组顺序使奇数位于偶数前面)。

我的思路,另外建立一个奇数链表odd,以及一个偶数链表even 遍历原来的链表,奇数位置连接在odd之后,偶数位置连接在even之后。再将even记载odd最后一个节点之后,返回odd的头结点。

package medium;

import dataStructure.ListNode;

public class L328OddEvenLinkedList {
// 空间复杂度为o(n)
public ListNode oddEvenList(ListNode head) {
// even 偶数 odd奇数
if (head == null)
return head;
ListNode odd = new ListNode(0);
ListNode odd1 =odd;
// 先用0占一个头结点,之后直接越过去。
ListNode even = new ListNode(0);
ListNode even1 = even;
int count = 1;
ListNode cur=head;
while (cur!= null) {
if (count % 2 == 0) {
even1.next = cur;
System.out.println("even偶数"+even1.val);
even1 = even1.next;
} else {
odd1.next = cur;
System.out.println("odd奇数"+odd1.val);
odd1 = odd1.next;
}
count++;
cur = cur.next;
}
even1.next=null;
odd1.next = even.next;
return odd.next;
} public static void main(String[] args) {
ListNode head1 = new ListNode(1);
ListNode head2 = new ListNode(2);
ListNode head3 = new ListNode(3);
ListNode head4 = new ListNode(4);
ListNode head5 = new ListNode(5);
ListNode head6 = new ListNode(6);
ListNode head7 = new ListNode(7);
ListNode head8 = new ListNode(8);
ListNode head9 = new ListNode(9);
head1.next = head2;
head2.next = head3;
head3.next = head4;
head4.next = head5;
head5.next = head6;
head6.next = head7;
head7.next = head8;
head8.next = head9;
head9.next = null;
L328OddEvenLinkedList l32 = new L328OddEvenLinkedList();
ListNode head = l32.oddEvenList(head1);
while (head != null) {
System.out.println(head.val);
head = head.next;
}
}
}

【Leetcode】 328. Odd Even Linked List的更多相关文章

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

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

  2. 【leetcode】328. Odd Even Linked List

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

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

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

  4. 【LeetCode】975. Odd Even Jump 解题报告(C++)

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

  5. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  6. 【leetcode】Intersection of Two Linked Lists(easy)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  7. 【LeetCode】Intersection of Two Linked Lists(相交链表)

    这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...

  8. 【LeetCode】链表 linked list(共34题)

    [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...

  9. <LeetCode OJ> 328. Odd Even Linked List

    328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a sin ...

随机推荐

  1. sqli-labs (less-8-less-10)

    盲注需要掌握一些MySQL的相关函数:length(str):返回str字符串的长度.substr(str, pos, len):将str从pos位置开始截取len长度的字符进行返回.注意这里的pos ...

  2. stm32串口中断总结

    串口文件uart.c需要被用到; 串口通信是对GPIO端口引脚的功能复用,因此需要用到gpio.c; 因为中断的产生,因此中断文件也是需要用到的: 中断响应函数需要自己编写: 接收中断:在接收移位寄存 ...

  3. Mac使用bootcamp安装win系统花屏解决方法

    15年11'乞丐版air装win屏幕花屏,很郁闷,先后找了网上很多方法,最终总结出了一个比较折中的方法,不玩游戏不使用大型3D的可以参考. 1 花屏现象 2 解决方法 2.1 禁用驱动 2.2 使用M ...

  4. uva 156 - Ananagrams (反片语)

    csdn:https://blog.csdn.net/su_cicada/article/details/86710107 例题5-4 反片语(Ananagrams,Uva 156) 输入一些单词,找 ...

  5. spark 例子count(distinct 字段)

    spark 例子count(distinct 字段) 例子描述: 有个网站访问日志,有4个字段:(用户id,用户名,访问次数,访问网站) 需要统计: 1.用户的访问总次数去重 2.用户一共访问了多少种 ...

  6. [BZOJ2127]happiness-[网络流-最小割]

    Description 传送门 Solution 按照最小割的思路考虑. 根据题意,当两个人都选文(理),需要砍掉两个人都选理(文)的加成:如果两个人选的不一样,就要都砍掉. 这是一个网络流建模的套路 ...

  7. 【转载】Ogre3d 2.1 源码编译安装教程

    原文:Ogre3d 2.1 源码编译安装教程 今年是3D手游年,今年也是游戏引擎战争进入白热的一年. 移动游戏的红海时代,让各大端游也忍不住纷纷伸出大白腿,Unreal.CryEngine纷纷宣布自己 ...

  8. springboot-vue-JWT使用

    springboot-vue-JWT使用 后端引入依赖: <dependency> <groupId>io.jsonwebtoken</groupId> <a ...

  9. php编程知识点2018

    一 .PHP基础部分 1.PHP语言的一大优势是跨平台,什么是跨平台? PHP的运行环境最优搭配为Apache+MySQL+PHP,此运行环境可以在不同操作系统(例如windows.Linux等)上配 ...

  10. Jmeter接口测试(三)接口测试实践

    Jmeter 脚本编写一般分五个步骤: 1. 添加线程组 2. 添加 http 请求 3. 在 http 请求中写入接入 url.路径.请求方式和参数 4. 添加查看结果树 5. 调用接口.查看返回值 ...