其实一开始并没有想到时间上O(n)的方法,想到了也是空间复杂度是O(n)的(需要用到栈或者递归):链表分两段,用栈记录第一段的遍历过程。

后来经提示想到了,可以将第二段链表逆序。从而不需要额外的辅助空间,在O(n)完成。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
//Divide the list into two parts first length n-n/2, second length n/2, reverse serconde part.
//Insert each node in second list into first list.
void reorderList(ListNode *head) {
if(NULL == head || NULL == head -> next) return; //divide
ListNode *p1 = head; ListNode* p2 = head -> next;
for(; p2 != NULL;p1 = p1 -> next, p2 = p2 -> next){
p2 = p2 -> next;
if(p2 == NULL) break;
}
ListNode* temp = p1;
p1 = p1 -> next;
temp -> next = NULL; //reverse second list
ListNode *pre = p1;
if(p1 -> next != NULL){
ListNode* p = p1 -> next;
temp = NULL;
p1 -> next = NULL;
while(p != NULL){
temp = p;
p = p -> next;
temp -> next = pre;
pre = temp;
}
}
ListNode* secHead = pre; //Insert node in second list into first list
ListNode* p = head;
ListNode* temp2 = NULL;
while(secHead != NULL){
temp = p -> next;
temp2 = secHead;
p -> next = temp2;
secHead = secHead -> next;
temp2 -> next = temp;
p = temp;
}
}
};

含测试的代码:

#include<stdio.h> 

struct ListNode {
int m_nValue;
ListNode *next;
ListNode(int x) : m_nValue(x), next(NULL) {}
ListNode(){} }; ListNode* CreateListNode(int value)
{
ListNode* pNode = new ListNode();
pNode->m_nValue = value;
pNode->next = NULL; return pNode;
} void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
if(pCurrent == NULL)
{
printf("Error to connect two nodes.\n");
//exit(1);
} pCurrent->next = pNext;
} void PrintListNode(ListNode* pNode)
{
if(pNode == NULL)
{
printf("The node is NULL\n");
}
else
{
printf("The key in node is %d.\n", pNode->m_nValue);
}
} void PrintList(ListNode* pHead)
{
printf("PrintList starts.\n"); ListNode* pNode = pHead;
while(pNode != NULL)
{
printf("%d\t", pNode->m_nValue);
pNode = pNode->next;
} printf("\nPrintList ends.\n");
} void DestroyList(ListNode* pHead)
{
ListNode* pNode = pHead;
while(pNode != NULL)
{
pHead = pHead->next;
delete pNode;
pNode = pHead;
}
} void AddToTail(ListNode** pHead, int value)
{
ListNode* pNew = new ListNode();
pNew->m_nValue = value;
pNew->next = NULL; if(*pHead == NULL)
{
*pHead = pNew;
}
else
{
ListNode* pNode = *pHead;
while(pNode->next != NULL)
pNode = pNode->next; pNode->next = pNew;
}
} void RemoveNode(ListNode** pHead, int value)
{
if(pHead == NULL || *pHead == NULL)
return; ListNode* pToBeDeleted = NULL;
if((*pHead)->m_nValue == value)
{
pToBeDeleted = *pHead;
*pHead = (*pHead)->next;
}
else
{
ListNode* pNode = *pHead;
while(pNode->next != NULL && pNode->next->m_nValue != value)
pNode = pNode->next; if(pNode->next != NULL && pNode->next->m_nValue == value)
{
pToBeDeleted = pNode->next;
pNode->next = pNode->next->next;
}
} if(pToBeDeleted != NULL)
{
delete pToBeDeleted;
pToBeDeleted = NULL;
}
} void DestroyNode(ListNode* pNode)
{
delete pNode;
pNode = NULL;
} void reorderList(ListNode *head) {
if(NULL == head || NULL == head -> next) return; //divide
ListNode *p1 = head; ListNode* p2 = head -> next;
for(; p2 != NULL;p1 = p1 -> next, p2 = p2 -> next){
p2 = p2 -> next;
if(p2 == NULL) break;
}
ListNode* temp = p1;
p1 = p1 -> next;
temp -> next = NULL; //reverse second list
ListNode *pre = p1;
if(p1 -> next != NULL){
ListNode* p = p1 -> next;
temp = NULL;
p1 -> next = NULL;
while(p != NULL){
temp = p;
p = p -> next;
temp -> next = pre;
pre = temp;
}
}
ListNode* secHead = pre; //Insert node in second list into first list
ListNode* p = head;
ListNode* temp2 = NULL;
while(secHead != NULL){
temp = p -> next;
temp2 = secHead;
p -> next = temp2;
secHead = secHead -> next;
temp2 -> next = temp;
p = temp;
}
} int main(){
ListNode* pNode1 = CreateListNode();
ListNode* pNode2 = CreateListNode();
ListNode* pNode3 = CreateListNode();
ListNode* pNode4 = CreateListNode();
ListNode* pNode5 = CreateListNode();
ListNode* pNode6 = CreateListNode();
ListNode* pNode7 = CreateListNode(); ConnectListNodes(pNode1, pNode2);
ConnectListNodes(pNode2, pNode3);
ConnectListNodes(pNode3, pNode6);
ConnectListNodes(pNode4, pNode5);
ConnectListNodes(pNode5, pNode6);
ConnectListNodes(pNode6, pNode7); PrintList(pNode1);
reorderList(pNode1);
PrintList(pNode1); DestroyNode(pNode1);
DestroyNode(pNode2);
DestroyNode(pNode3);
DestroyNode(pNode4);
DestroyNode(pNode5);
DestroyNode(pNode6);
DestroyNode(pNode7); return ;
}

链表系列 - [LeetCode] 链表的交错重排L1,Ln,L2,Ln-1 ....的更多相关文章

  1. Spark2.0机器学习系列之12: 线性回归及L1、L2正则化区别与稀疏解

    概述 线性回归拟合一个因变量与一个自变量之间的线性关系y=f(x).       Spark中实现了:       (1)普通最小二乘法       (2)岭回归(L2正规化)       (3)La ...

  2. Leetcode算法系列(链表)之删除链表倒数第N个节点

    Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...

  3. Leetcode算法系列(链表)之两数相加

    Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...

  4. [leetcode]143. 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 may not mod ...

  5. (链表 importance) leetcode 2. Add Two Numbers

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  6. LeetCode链表解题模板

    一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...

  7. LeetCode链表相加-Python<二>

    上一篇:LeetCode两数之和-Python<一> 题目:https://leetcode-cn.com/problems/add-two-numbers/description/ 给定 ...

  8. leetcode 链表类型题总结

    链表测试框架示例: // leetcodeList.cpp : 定义控制台应用程序的入口点.vs2013 测试通过 // #include "stdafx.h" #include ...

  9. leetcode: 链表2

    1. copy-list-with-random-pointer(拷贝一个带随机指针的链表) A linked list is given such that each node contains a ...

随机推荐

  1. 图像质量评价指标之 PSNR 和 SSIM

    1. PSNR (Peak Signal-to-Noise Ratio) 峰值信噪比 给定一个大小为 \(m×n\) 的干净图像 \(I\) 和噪声图像 \(K\),均方误差 \((MSE)\) 定义 ...

  2. RedHat/CentOS利用iso镜像做本地yum源

    在这里用iso或者光盘做本地yum源的方法是差不多的,只是用光盘的话Linux系统会自动挂载,用iso镜像的或需要手动挂载,这里就说挂载iso的方法吧. (1) 创建iso存放目录和挂载目录 mkdi ...

  3. SDUST OJ 时间类的加、减法赋值运算

    Problem F: 时间类的加.减法赋值运算 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 3801  Solved: 2210[Submit][St ...

  4. python学习笔记07:自定义类型

    class person: def __init__(self,name,age,weight): self.name = name self.age = age self.weight = weig ...

  5. lintcode-14-二分查找

    二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 在数组 ...

  6. sublime Text3 如何自动排版代码

    安装 html beautiful 然后按ctrl+shift+alt+f

  7. 关于&$地址传递的练习

    php默认为传值传递: 既: $a=10;$b=$a; //$b为10$a=+10; //$a 为20 echo $a.'和'.$b;  # $a is 20 and $b is 10! 要是想变为地 ...

  8. 第61天:json遍历和封装运动框架(多个属性)

    一.json 遍历  for in  关键字  for ( 变量 in  对象)  { 执行语句;  } 例如: var json = {width:200,height:300,left:50}co ...

  9. Windows 8.1 SecureBoot未正确配置的解决方法

    使用联想Y510P,安装win8.1后破解 ,屏幕右下角老是显示 SecureBoot未正确配置的解决方法,以下是解决方案 步骤1:在机器重启至“Lenovo字样的屏幕”时,不停敲击“F2”键或“Fn ...

  10. 使用oledb读取excel表

    string path = "C:\\Users\\aaa\\Desktop\\aa.xls"; string conn = "Provider = Microsoft. ...