Odd Even Linked List

5-13 1,257 views

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 …

 

 

给定一个单向链表,将奇数节点放在前面,偶数节点放在后面。所有奇数节点或偶数节点的相对位置保持不变。空间复杂度O(1),时间复杂度O(n)。所有奇数节点或偶数节点的相对位置保持不变。

 

 

 

 

 

标签:

Move Zeroes

Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements. For exa...

阅读全文

Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space...

阅读全文

八皇后问题与回溯算法

八皇后问题是在8*8的棋盘上放置8枚皇后,使得棋盘中每个纵向、横向、左上至右下斜向、右上至左下斜向均只有一枚皇后。八皇后的一个可行解如图所示: ...

阅读全文