LeetCode 328. Odd Even Linked List C#
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 ...
Solution 1):

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode OddEvenList(ListNode head) {
if(head==null)
{
return head;
}
ListNode odd = head;
ListNode even = head.next;
while(even!=null&&even.next!=null)
{
ListNode oddNext = odd.next;
ListNode evenNext = even.next;
even.next = evenNext.next;
odd.next = evenNext;
evenNext.next = oddNext;
odd = odd.next;
even = even.next; }
return head;
}
}
Solution 2):
1->2->3->4->5->6->7 to odd: 1->3->5 and even 2->4->6;
then return odd ->even;
public class Solution {
public ListNode OddEvenList(ListNode head) {
if(head==null)
{
return head;
}
ListNode odd = head;
ListNode even = head.next;
ListNode evenHead = even;
while(even!=null&&even.next!=null)
{
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenHead;
return head;
}
}
LeetCode 328. Odd Even Linked List C#的更多相关文章
- [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)
每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...
- LeetCode 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- Java [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
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 1->2->3->4->5->NULL, return 1->3->5->2->4->NULL. 就是将序号为单数的放在前面,而 ...
- <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
一天一道LeetCode系列 (一)题目 Given a singly linked list, group all odd nodes together followed by the even n ...
- 【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 note her ...
随机推荐
- python打包成window可执行程序
python程序可以通过python hello.py执行,但是需要安装python的解释器,并配置环境变量,打包成exe程序之后可以直接执行. 使用setup工具和py2exe可以做到这一点. 最简 ...
- zTree应用实例详讲
zTree应用实例详讲(1) 因为项目的需要,要创建一棵动态的文件树,此树除了实现异步获取子节点外,还要实现对树节点的增.删.改.查.移动.重命名.批量删除.批量移动. 每一个操作都要和数据库打交道. ...
- 数据结构之树(Tree)(一) :树
ps:好久没用动手写blog了,要在这条路上不断发展,就需要不停的学习,不停的思考与总结,当把写blog作为一种习惯,就是自我成长的证明,Fighting!. 一.简介 树是一种重要的非线性数据结构, ...
- tag标签记录
看到项目代码中有一个自定义的tag标签,想起以前自己写过的标签,竟然忘记的差不多了,手一痒,自己写个简单的tag标签,回顾一下历史知识 首先建一个servlet工程,然后写个index.jsp,项目跑 ...
- 死锁线程探讨Java中的死锁现象
题记:写这篇博客要主是加深自己对死锁线程的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 今天搞了一下Java的死锁机制,感到自己还是不怎么懂,所以就从一些简略的源代码中琢磨:我先 ...
- js Get中文乱码 转码
encodeURI escape post: location.href = "ActivitiesOnSale?CurrId=" + Cid + "&CNam ...
- Js-Html 前端系列--checkbox
今天搞全选按钮,设置Checkbox的时候,处于Checked状态但是不显示勾.最后得出解决方案: var c = boxcList.eq(i).attr("checked"); ...
- MySQL闪回原理与实战
本文将介绍闪回原理,给出笔者的实战经验,并对现存的闪回工具作比较. DBA或开发人员,有时会误删或者误更新数据,如果是线上环境并且影响较大,就需要能快速回滚.传统恢复方法是利用备份重搭实例,再应用去除 ...
- 学习OpenCV,看这些!
OpenCV简介: OpenCV 是一款功能强大的跨平台计算机视觉开源库,可以用于解决人机交互.物体检测.人脸识别等领域的问题.库本身是采用 C++ 编写的,但是同时也对 Python, Java, ...
- scrapy在ubuntu上安装总结
此文档是本人学习时使用的,采用一个实例作为引导进行安装测试. 实例下载地址如下: https://github.com/sans-serif/scrapy-german-news#introducti ...