LeetCode 328
Odd Even Linked List
Problem Description:
将链表的所有奇数结点按照原顺序排在所有偶数结点前面。
具体的题目信息:
https://leetcode.com/problems/odd-even-linked-list/description/Solution:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if (head == NULL)
return head;
if (head->next == NULL)
return head;
ListNode* p = head;
ListNode* q = head->next;
ListNode* temp = head->next;
while(q && q->next) {
ListNode* r = q->next->next;
q->next->next = temp;
p->next = q->next;
p = p->next;
q->next = r;
q = q->next;
}
return head;
}
};