链表系列 - [LeetCode] 链表的交错重排L1,Ln,L2,Ln-1 ....
其实一开始并没有想到时间上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 ....的更多相关文章
- Spark2.0机器学习系列之12: 线性回归及L1、L2正则化区别与稀疏解
概述 线性回归拟合一个因变量与一个自变量之间的线性关系y=f(x). Spark中实现了: (1)普通最小二乘法 (2)岭回归(L2正规化) (3)La ...
- Leetcode算法系列(链表)之删除链表倒数第N个节点
Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...
- Leetcode算法系列(链表)之两数相加
Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...
- [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 ...
- (链表 importance) leetcode 2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode链表解题模板
一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...
- LeetCode链表相加-Python<二>
上一篇:LeetCode两数之和-Python<一> 题目:https://leetcode-cn.com/problems/add-two-numbers/description/ 给定 ...
- leetcode 链表类型题总结
链表测试框架示例: // leetcodeList.cpp : 定义控制台应用程序的入口点.vs2013 测试通过 // #include "stdafx.h" #include ...
- leetcode: 链表2
1. copy-list-with-random-pointer(拷贝一个带随机指针的链表) A linked list is given such that each node contains a ...
随机推荐
- Reject Inference: Your Data is Deceiving You
Keyword: Reject Inference Suppose there is a dataset of several attributes, including working condit ...
- 贵州省未来二十年的投资机会的探讨2>
房产投资 升值最快的 在教育资源丰富 生活方便的 地方 价格和地段取其中之一. 其次 车位 再其次墓地等 公寓住房. 还有商标 和网站注册 公司注册 除了以上的这些 还有茅台生效酒 收藏
- Ext JS 6学习文档-第5章-表格组件(grid)
Ext JS 6学习文档-第5章-表格组件(grid) 使用 Grid 本章将探索 Ext JS 的高级组件 grid .还将使用它帮助读者建立一个功能齐全的公司目录.本章介绍下列几点主题: 基本的 ...
- 机器人控制tcp通信参数调优
机器人使用WiFi通信,实现指令下传,状态上传.而WiFi信道平时带宽较稳定,但会在某些时候突然中断,造成ping的延时较高,但可以马上恢复.如果一直ping,则一般情况下ping值很小,但长时间(数 ...
- Python练习——循环2
1.求1~100之间能被7整除,但不能同时被5整除的所有整数 . for i in range(1,101): if i%7 == 0 and i%5 !=0: print(i) 2.输出“水仙花数” ...
- linux下php环境配置
参: http://www.laozuo.org/5542.html LAMP,基于Linux/Apache/MySQL/PHP架构的网站建设环境,对于一般的网站来说足够使用,如果我们的网站访问量或者 ...
- <Effective C++>读书摘要--Resource Management<二>
<Item 15> Provide access to raw resources in resource-managing classes 1.You need a way to con ...
- init只创建一次 只有父类的init创建servletContext的对象
init只创建一次 只有父类的init创建servletContext的对象 如果重写父类的方法 但不显示调用父类的init 是不会创建servletContext对象的
- table 标签 语法
- bzoj3992-序列统计
给出\(n,m,x,S\),其中\(S\subseteq [0,m)\),问有多少个长度为\(n\)的数列\(a\)使得\(a_i\in S\),并且数列中所有元素的乘积mod \(m\)为\(x\) ...