【链表】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 ...
思路:
这道题只要将奇数和偶数节点拿出来组成两个链表,然后合并即可。
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var oddEvenList = function(head) {
if(head==null||head.next==null){
return head;
} var p=head.next.next,oHead=head,eHead=head.next,i=3,op=oHead,ep=eHead;
while(p!=null){
if(i%2!=0){
op.next=p;
op=op.next;
}else{
ep.next=p;
ep=ep.next;
}
p=p.next;
i++;
} ep.next=null;
op.next=eHead;
return oHead;
};
【链表】Odd Even Linked List的更多相关文章
- LeetCode 328. 奇偶链表(Odd Even Linked List)
328. 奇偶链表 328. Odd Even Linked List 题目描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是 ...
- [Swift]LeetCode328. 奇偶链表 | Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- LeetCode 328:奇偶链表 Odd Even Linked List
给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝试使用原地算法完成.你的算法的空间复杂度应为 O(1), ...
- 【Leetcode】 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- <LeetCode OJ> 328. Odd Even Linked List
328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a sin ...
- [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)
每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...
- 单链表反转(Singly Linked Lists in Java)
单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 package dsa.linkedlist; public class Node<E> ...
- [LeetCode] Odd Even Linked List 奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- (链表) leetcode 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
随机推荐
- (KMP 根据循环节来计算)Period -- hdu -- 1358
http://acm.hdu.edu.cn/showproblem.php?pid=1358 Period Time Limit: 2000/1000 MS (Java/Others) Memo ...
- python 的几种启动方式
python 的几种启动方式 (1)利用Win的操作系统的:命令行工具 cmd.exe Win + R 调出运行对话框,然后输入cmd,即可调出“命令提示符对话框” 或者 在菜单中店家附件中的命令提 ...
- Americans are usually tolerant (Listen speak of Unit 2)
Americans are usually 1) tolerant of non-native speakers who have some 2) trouble understanding Engl ...
- EBS R12 探索之路【EBS 经典SQL分享】
http://bbs.erp100.com/thread-251217-1-1.html 1. 查询EBS 系统在线人数 SELECT U.USER_NAME ,APP.APPLICATION_SHO ...
- 论文笔记(2)-Dropout-Regularization of Neural Networks using DropConnect
这篇paper使用DropConnect来规则化神经网络.dropconnect和dropout的区别如下图所示.dropout是随机吧隐含层的输出清空,而dropconnect是input unit ...
- [ACM_数据结构] 线段树模板
#include<iostream> #include<cmath> using namespace std; #define maxn 200005 class Node{ ...
- TSQL--删除登陆相关的用户
无二话,上代码 --删除登陆相关的用户 --遍历所有数据库,查找到与登录名相关联的的用户,生成删除脚本 ) SET @loginName='DBA'; DECLARE @comm NVARCHAR(M ...
- SqlServer数据库同时备份到两台服务器上(并自动删除过期文件)
数据库同时备份到两台服务器上(并自动删除过期文件) 举例 :(本地)服务器A: IP :192.168.1.1 (远程)服务器B: IP :192.168.1.2 数据库版本:SqlServer200 ...
- NetCore入门篇:(五)Net Core项目使用静态文件
一.简介 1.Net Core默认无法访问静态文件,需要在Startup通过代码添加定义. 2.本文介绍两种静态文件目录实现方式. 二.启用默认目录 1.添加图片文件 2.测试访问结果(不能访问) 3 ...
- .net下使用最小堆实现TopN算法
测试代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...