您的位置:首页 > 其它

328. Odd Even Linked List

2016-02-19 14:36 393 查看
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 ...

Credits:

Special thanks to @DjangoUnchained for adding this problem and creating all test cases.

解题思路:

题目意思是将原来链表中的奇数位置按顺序放在前面,链表中偶数位置的元素紧接着奇数后面。可以维护两个指针odd和even,指向奇数链表头部和偶数链表头部,遍历链表,分别将奇偶元素添加到两个链表中,最后合并。注意要将偶数链表的尾部指向null。

#include<windows.h>

#include<iostream>

using namespace std;

struct ListNode {

int val;

ListNode *next;

ListNode(int x) : val(x), next(NULL) {}

};

class Solution {

public:

ListNode* oddEvenList(ListNode* head) {

if(head==NULL||head->next==NULL)return head;

ListNode*odd=head;

ListNode*even=head->next;

ListNode*p=odd;

ListNode*q=even;

ListNode*r=head->next->next;

int cnt=1;

while(r!=NULL){

if(cnt&1){

p->next=r;

p=p->next;

}

else{

q->next=r;

q=q->next;

}

r=r->next;

cnt++;

}

q->next=NULL;

p->next=even;

return odd;

}

};

int main(){

Solution test=Solution();

ListNode*head=new ListNode(1);

ListNode*p=head;

p->next=new ListNode(2);

p=p->next;

p->next=new ListNode(3);

p=p->next;

p->next=new ListNode(4);

p=p->next;

p->next=new ListNode(5);

p=test.oddEvenList(head);

while(p!=NULL){

cout<<p->val<<" ";

p=p->next;

}

cout<<endl;

system("pause");

return 0;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: