Leetcode--Reorder List
2014-08-15 21:57
337 查看
Problem Description:
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes' values.
For example,
Given
分析:按照要求交换链表的元素,第一种方法,直接遍历一遍链表将所有节点指针保存在vector中,然后一头一尾不断重建链表即可。具体代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode *head) {
ListNode *p=head;
int length,i=1,tag=0;
vector<ListNode* > array;
while(p)
{
array.push_back(p);
p=p->next;
}
p=head;
if(array.size()<3) return;
length=array.size()-1;
while(tag<array.size()/2)
{
p->next=array[length--];
p=p->next;
p->next=array[i++];
p=p->next;
tag++;
}
p->next=NULL;
}
};
第二种方法就是找到链表的中间节点,将链表分为两半,然后将后半部分转置,最后将前后两部分进行合并即可,具体代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode *head) {
if (!head){return;}
if (head->next==NULL){return;}
ListNode *p=head;
ListNode *q=head;
//find the midddle pointer
while (q->next && q->next->next){
p=p->next;
q=q->next->next;
}
//now p is middle pointer
//reverse p->next to end
q = p->next;
while (q->next){
ListNode* tmp = p->next;
p->next = q->next;
q->next = q->next->next;
p->next->next = tmp;
}
//reorder
q = head;
while (p!=q && p->next){
ListNode* tmp = q->next;
q->next = p->next;
p->next = p->next->next;
q->next->next = tmp;
q=q->next->next;
}
return;
}
};
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes' values.
For example,
Given
{1,2,3,4}, reorder it to
{1,4,2,3}.
分析:按照要求交换链表的元素,第一种方法,直接遍历一遍链表将所有节点指针保存在vector中,然后一头一尾不断重建链表即可。具体代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode *head) {
ListNode *p=head;
int length,i=1,tag=0;
vector<ListNode* > array;
while(p)
{
array.push_back(p);
p=p->next;
}
p=head;
if(array.size()<3) return;
length=array.size()-1;
while(tag<array.size()/2)
{
p->next=array[length--];
p=p->next;
p->next=array[i++];
p=p->next;
tag++;
}
p->next=NULL;
}
};
第二种方法就是找到链表的中间节点,将链表分为两半,然后将后半部分转置,最后将前后两部分进行合并即可,具体代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode *head) {
if (!head){return;}
if (head->next==NULL){return;}
ListNode *p=head;
ListNode *q=head;
//find the midddle pointer
while (q->next && q->next->next){
p=p->next;
q=q->next->next;
}
//now p is middle pointer
//reverse p->next to end
q = p->next;
while (q->next){
ListNode* tmp = p->next;
p->next = q->next;
q->next = q->next->next;
p->next->next = tmp;
}
//reorder
q = head;
while (p!=q && p->next){
ListNode* tmp = q->next;
q->next = p->next;
p->next = p->next->next;
q->next->next = tmp;
q=q->next->next;
}
return;
}
};
相关文章推荐
- leetcode Reorder List
- LEETCODE Reorder List
- leetcode:Reorder List
- leetcode之Reorder List
- LeetCode(143)Reorder List
- leetCode刷题Reorder_list (快速求出链表的中间结点——快慢指针方法)
- 60_leetcode_Reorder List
- Reorder List|leetcode题解
- LeetCode:Reorder List
- LeetCode:Reorder List
- leetcode题目:Reorder List
- 【Leetcode】Reorder List
- [Leetcode] Reorder list 重排链表
- LeetCode Reorder List
- [leetcode-143]Reorder List(c)
- 【LeetCode with Python】 Reorder List
- LeetCode | Reorder List
- LeetCode:Reorder List
- LeetCode:Reorder List
- LeetCode 之 Reorder List