【Leetcode】 328. Odd Even Linked List
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的更多相关文章
- 【LeetCode】328. Odd Even Linked List 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】328. Odd Even Linked List
题目如下: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...
- 【一天一道LeetCode】#328 Odd Even Linked List
一天一道LeetCode系列 (一)题目 Given a singly linked list, group all odd nodes together followed by the even n ...
- 【LeetCode】975. Odd Even Jump 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【leetcode】Intersection of Two Linked Lists
题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- 【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 ...
- 【LeetCode】Intersection of Two Linked Lists(相交链表)
这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...
- 【LeetCode】链表 linked list(共34题)
[2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...
- <LeetCode OJ> 328. Odd Even Linked List
328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a sin ...
随机推荐
- 《Linux命令学习手册》系列分享专栏
<Linux命令学习手册>系列分享专栏 <Linux命令学习手册>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/207 ...
- 最短寻道优先算法(SSTF)——磁盘调度管理
原创 最近操作系统实习,敲了实现最短寻道优先(SSTF)——磁盘调度管理的代码. 题目阐述如下: 设计五:磁盘调度管理 设计目的: 加深对请求磁盘调度管理实现原理的理解,掌握磁盘调度算法. 设计内容: ...
- 2017-2018-1 20155307 《信息安全系统设计基础》第san周学习总结
2017-2018-1 20155307 <信息安全系统设计基础>第三周学习总结 教材学习内容总结 无符号数和有符号数的特性,数字如何表示,IEEE标准,浮点数格式.(我感觉我把课下测试做 ...
- 【LG3723】[AHOI2017/HNOI2017]礼物
[LG3723][AHOI2017/HNOI2017]礼物 题面 洛谷 题解 首先我们将\(c\)看作一个可以为负的整数,那么我们就可以省去讨论在哪个手环加\(c\)的繁琐步骤了 设我们当前已经选好了 ...
- Android:反编译apk
一.所需工具 1. apktool (1)作用:获取资源文件,例如图片.布局文件 (2)下载地址:https://bitbucket.org/iBotPeaches/apktool/downloads ...
- 【一】H.264/MPEG-4 Part 10 White Paper 翻译之 Overview of H.264
翻译版权所有,转载请注明出处~ xzrch@2018.09.14 ------------------------------------------------------------------- ...
- 那些年安装Appium遇到的坑
安装appium以及相关的总体记录 1 主要流程是参照这个来 https://www.cnblogs.com/wangyinghao/p/5780151.html 细节参考虫师的博客 http ...
- Linux 安装Redis<准备>(使用Mac远程访问)
阅读本文需要一定的Linux基础 一 Redis简介 redis是用c语言编写的一款开源的高性能键值对(key-value)数据库 它通过提供多种键值数据类型来适应不同场景下的存储需求 二 Redis ...
- Unity Lighting - Light Probes 光照探针(十)
Light Probes 光照探针 Only static objects are considered by Unity’s Baked or Precomputed Realtime GI s ...
- Kubernetes v1.10----部署kubernetes-dashboard v1.83
Kubernetes v1.10----部署kubernetes-dashboard v1.83 1.下载 kubernetes-dashboard yaml文件 #因为文件中的image指定的是谷 ...