【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 1:
Input:1->2->3->4->5->NULL
Output:1->3->5->2->4->NULLExample 2:
Input: 2->1->3->5->6->4->7->NULL
Output:2->3->6->7->1->5->4->NULLNote:
- 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 ...
解题思路:本题难度不大,分别维护奇/偶节点的两个头指针,然后遍历链表,分别将指针指向对方的next,指向后再把自己移动到之前指向的next即可。
代码如下:
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None or head.next == None:
return head
evenHead = even = head.next
odd = head
while odd != None and even != None:
odd.next = even.next
if odd.next != None:
odd = odd.next
even.next = odd.next
even = even.next
odd.next = evenHead
return head
【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 note her ...
- 【一天一道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 ...
随机推荐
- 【leetcode】sudokuSolver数独解题
0.摘要 小时候在报纸上玩过数独,那时候觉得很难,前几天在leetcode上遇到了这个题,挺有意思于是记录下来 一般一道数独题,就像他给的例子这样,9*9的格子,满足 行,列 ,宫均取1-9的数,切互 ...
- JavaSE---多线程---线程组
1.概述 1.1 Java中使用ThreadGroup表示线程组,可以对一批线程进行分类管理: Java运行程序直接对 线程组 进行控制,对线程组的控制相当于 同时控制这组线程: 一旦线程加入某一组 ...
- mui.min.js:7 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load
mui框架做的微信公众号网页,在上传数据的时候报了这个错,async: true,//将false改为true就可以了 https://blog.csdn.net/liuzp111/article/d ...
- [CSP-S模拟测试45]题解
开局一行$srand$,得分全靠随机化. A.kill 发现两个并不显然的性质: 1.选中的人和怪物一定是按顺序的.第一个人打所有被选中怪物的第一只,第二个人打第二只,$etc$. 2.最优方案打的怪 ...
- [NOIP模拟25]题解
A.字符串 Catalan数不能再裸了 #include<cstdio> #include<iostream> #include<cstring> using na ...
- 修改 DbVisualizer 自动完成快捷键
1.找到 DbVisualizer 安装目录 lib目录 下的 dbvis.jar 包. 2.使用 WinRaR 打开dbvis.jar包,编辑 dbvis-actions.xml 文件(解压或直接修 ...
- HTML5: HTML5 表单属性
ylbtech-HTML5: HTML5 表单属性 1.返回顶部 1. HTML5 表单属性 HTML5 新的表单属性 HTML5 的 <form> 和 <input>标签添加 ...
- cs224d 作业 problem set2 (一) 用tensorflow纯手写实现sofmax 函数,线性判别分析,命名实体识别
Hi Dear Today we will use tensorflow to implement the softmax regression and linear classifier algor ...
- upc组队赛3 Congestion Charging Zon【模拟】
Congestion Charging Zon 题目描述 Tehran municipality has set up a new charging method for the Congestion ...
- vue入门例子
vue入门例子 1.声明示渲染 {{message}} 2.绑定事件 v-bind 3.控制切换一个程序是否显示 v-if 4.渲染循环 ...