[LeetCode] 重排链表 reorder linked list

Given a singly linked list LL0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-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}.

Analysis:


Let's see some examples:

{1,2,3,4,5,6} ---> {1,6,2,5,3,4}
{1,2,3,4,5,6,7} ---> {1,7,2,6,3,5,4}

One straightforward middle step of such reordering is:
{1,2,3,4,5,6}  --> {1,2,3, 6,5,4 } --> {1, 6 ,2, 5 ,3, 4 }
{1,2,3,4,5,6,7}---> {1,2,3,4, 7,6,5 } ---> {1, 7 ,2, 6 ,3, 5 ,4}

By reversing the last part of the linked list, we do not need to worried about the "parent" pointer anymore. The final step is just insert the each element in the last part into the first part (every two element).

So the algorithm implemented below can be summarized as:
Step 1  Find the middle pointer of the linked list (you can use the slow/fast pointers)
Step 2  Reverse the second part of the linked list (from middle->next to the end)
Step 3  Do the reordering. (inset every element in the second part in between the elements in the first part)

下面是代码。要注意的是:1)第24和25行,fast pointer被初始化成slow pointer的下一个。(自己在纸上画一下,这样初始化的结果是什么)。不能把fast pointer 和slow pointer都初始化成head节点。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#include <string.h>

#include <stack>
#include <stdio.h>
#include <stdlib.h>

struct ListNode {
     int data;
     ListNode *next;
     ListNode(int x, ListNode* nex) : data(x), next(nex) {}
};

ListNode* reverseList(ListNode* head);
void printLL(ListNode* head);

void reorderList(ListNode *head) {
	if(head==NULL || head->next == NULL || head->next->next==NULL)
		return;

	ListNode* slow = head;
	ListNode* fast = head->next; // fast should be the next of slow

	while(fast)
	{
		if(fast->next)	fast = fast->next;
		else break;

		if(fast->next) fast = fast->next;
		else break;		

		if(slow==NULL) slow = head;
		else slow = slow->next;
	}

	reverseList( slow->next );
	slow->next = NULL;

	ListNode* cur1 = head;
	ListNode* cur2 = fast;
	while(cur1)
	{
		ListNode* nex1 = cur1->next;
		ListNode* nex2 = cur2->next;

		cur1->next = cur2;
		if(nex1 == NULL)
		{
			cur2->next = nex2;
			break;
		}
		else
			cur2->next = nex1;

		cur1 = nex1;
		cur2 = nex2;	
	}	
}

ListNode* reverseList(ListNode* head)
// reverse a linked list
{
	if(head==NULL)
		return head;
 	if(head->next == NULL)
		return head;

	ListNode* tmp = reverseList(head->next);
	tmp->next = head;
	head->next = NULL;
	return head;
}

void printLL(ListNode* head)
{
	while(head)
	{
		cout<<head->data<<" ";
		head = head->next;
	}
}

int main()
{
	ListNode* n0 =new ListNode(0, NULL);
	ListNode* n1 =new ListNode(1, n0);
	ListNode* n2 =new ListNode(2, n1);
	ListNode* n3 =new ListNode(3, n2);
	ListNode* n4 =new ListNode(4, n3);
	ListNode* n5 =new ListNode(5, n4);
	ListNode* n6 =new ListNode(6, n5);
	ListNode* n7 =new ListNode(7, n6);

	reorderList(n7);
	printLL(n7);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值