LeetCode---Reorder List
题目:
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}
.
思考了一会,想到了以下办法:首先,将链表从中间分为两部分;然后将右半部逆序;最后交叉合并两部分链表。
所以可以说这道题可以分为三道:
1、定位一个链表的中间位置。
实现方法为,定义三个临时节点temp,middlepre,middle,初始值赋值为head,然后temp走两步,middle节点走一步,同时将将middle节点的前一个节点赋值给middlepre。temp走完后将middlepre的next置为空,此时便将链表分为了两部分,头节点分别为head和middle。
int length = 0;
ListNode* temp = head;
ListNode* middle = head;
ListNode* middlepre = head;
while (temp != NULL)
{
length++;
if (length % 2 == 0)
{
middlepre = middle;
middle = middle->next;
}
temp = temp->next;
}
if (length == 1)
{
return;
}
middlepre->next = NULL;
2、将单链表逆置。
这个是经常被考到的,逆置单链表而且不需要额外的空间,只是通过指针操作。要点主要有以下几个:
1)空链表或者只有一个节点的链表,直接返回。
2)定义临时节点temp,temp_next分别记录当前节点和当前节点的下一个节点,将temp初值赋值为head。
3)将第一个temp的next赋值为NULL。断开与后面的节点之间的联系,作为新链表的最后一个节点。
4)进入循环,当temp_next为NULL时推出循环,否则进行如下指针切换动作。
ListNode* temp_next_next = temp_next->next;//记录下一个节点的下一个节点
temp_next->next = temp;//逆置,将temp_next指向temp
temp = temp_next;//temp和temp_next向下走
temp_next = temp_next_next;
5)最后返回temp。这个返回值便是逆置的链表。
ListNode* reverseList(ListNode* head)
{
if(!head || head->next == NULL)
{
return head;
}
ListNode *temp = head;
ListNode *temp_next = temp->next;
temp->next = NULL;
while (temp_next != NULL)
{
ListNode* temp_next_next = temp_next->next;
temp_next->next = temp;
temp = temp_next;
temp_next = temp_next_next;
}
return temp;
}
3、交叉合并两个单链表。
temp = head;//将head赋值给临时节点temp
ListNode* rightpart = reverseList(middle);//获取节点的右半部分逆置的链表
然后就是交叉替换节点的next指针指向,从而实现两个链表的交叉合并。
temp = head;
ListNode* rightpart = reverseList(middle);
ListNode* rightpart_next = rightpart->next;
ListNode* temp_next = temp->next;
while (temp_next != NULL)
{
temp->next = rightpart;
rightpart->next = temp_next;
temp = temp_next;
temp_next = temp->next;
rightpart = rightpart_next;
rightpart_next = rightpart->next;
}
if (rightpart != NULL)
{
temp->next = rightpart;
}
完整程序如下:
#include<iostream>
using namespace std;
struct ListNode{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* reverseList(ListNode* head)
{
if(!head || head->next == NULL)
{
return head;
}
ListNode *temp = head;
ListNode *temp_next = temp->next;
temp->next = NULL;
while (temp_next != NULL)
{
ListNode* temp_next_next = temp_next->next;
temp_next->next = temp;
temp = temp_next;
temp_next = temp_next_next;
}
return temp;
}
void reorderList(ListNode *head) {
if (!head)
{
return;
}
int length = 0;
ListNode* temp = head;
ListNode* middle = head;
ListNode* middlepre = head;
while (temp != NULL)
{
length++;
if (length % 2 == 0)
{
middlepre = middle;
middle = middle->next;
}
temp = temp->next;
}
if (length == 1)
{
return;
}
middlepre->next = NULL;
temp = head;
ListNode* rightpart = reverseList(middle);
ListNode* rightpart_next = rightpart->next;
ListNode* temp_next = temp->next;
while (temp_next != NULL)
{
temp->next = rightpart;
rightpart->next = temp_next;
temp = temp_next;
temp_next = temp->next;
rightpart = rightpart_next;
rightpart_next = rightpart->next;
}
if (rightpart != NULL)
{
temp->next = rightpart;
}
}
};
int main()
{
ListNode* first = new ListNode(1);
ListNode* second = new ListNode(2);
ListNode* third = new ListNode(3);
//ListNode* forth = new ListNode(4);
first->next = second;
second->next = third;
//third->next = forth;
Solution sol;
sol.reorderList(first);
return 0;
}