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. PHP 审计

    1.get  和post  上传数组,数组的sha1值相等 通过阅读代码,我们发现要想得到flag就要达到下面三个条件: 使 uname的sha1值 与 passwd的sha1的值 相等    但是同 ...

  2. Leecode刷题之旅-C语言/python-112 路径总和

    /* * @lc app=leetcode.cn id=112 lang=c * * [112] 路径总和 * * https://leetcode-cn.com/problems/path-sum/ ...

  3. 20155229——实验五《 Java网络编程及安全》

    20155229--实验五 Java网络编程及安全 实验内容 实验一: 两人一组结对编程: 参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA ...

  4. 20155310 《JAVA程序设计》实验二(JAVA面向对象程序设计)实验报告

    20155310 <JAVA程序设计>实验二(JAVA面向对象程序设计)实验报告 实验内容 •初步掌握单元测试和TDD •理解并掌握面向对象三要素:封装.继承.多态 •初步掌握UML建模 ...

  5. 20155322 2016-2017-2 《Java程序设计》 第一周学习总结

    20155322 2016-2017-2 <Java程序设计> 第一周学习总结 教材学习内容总结 本周学习内容的主要是: 一.浏览教材,根据自己的理解每章提出一个问题. 在浏览教材后,我提 ...

  6. kyligence enterprise3.2.x版本使用mysql作为数据源构建报错

    1.报错信息如下: exe cmd:null/bin/sqoop import -Dorg.apache.sqoop.splitter.allow_text_splitter=true -Dfs.de ...

  7. Mac下 Windows 7 虚拟机成功搭建SVN服务器后如何与Xcode建立联系,并上传原始工程的详细步骤

    内容中包含 base64string 图片造成字符过多,拒绝显示

  8. 使用 AFNetworking做过断点续传吗?

    断点续传的主要思路: 检查服务器文件信息 检查本地文件 如果比服务器文件小, 断点续传, 利用 HTTP 请求头的 content-range实现断点续传(如果content-range不存在就取Co ...

  9. 手撕一个 Galgame 神器——Shub-Niggurath Project

    一.想法 Galgame 我们大概可以分为好用的 Galgame 和好玩的 Galgame,但是如果你把好玩的 Galgame 拿来用的话,有时候会十分让人着急.如果你躺在床上,一只手还在按压键盘实际 ...

  10. cocos2dx2.0 帧动画的创建和播放过程 深入分析

    一.帧动画的创建过程帧动画的实现有四个不可或缺的类,如下:1.CCSpriteFrame:精灵帧信息.存储帧动画的每一帧的纹理基本信息. class CC_DLL CCSpriteFrame : pu ...