链表的反转、合并(不借助额外list数组)
链表的基本操作:线性表 (单链表、循环链表-python实现)
反转链表:
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution: def __init__(self,list):
self.head=ListNode(None)
self.list=list def listCreateForward(self):
temp = self.head
while self.list:
num = self.list.pop(0)
node = ListNode(num)
temp.next = node
temp = node
self.readList(self.head)
self.ReverseList(self.head) def ReverseList(self, pHead):
if pHead.next==None:
return pHead
head=ListNode(None)
while pHead.next.next!=None:
temp = pHead.next
pHead.next=pHead.next.next
temp.next=head.next
head.next=temp
node=pHead.next
node.next=head.next
head.next=node
self.readList(head) def readList(self,head):
temp =head
while temp.next != None:
temp = temp.next
print temp.val s=Solution([1,2,3,4,5,6])
s.listCreateForward()
合并链表:
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution: def __init__(self,list):
self.head=ListNode(None)
self.list=list def listCreateForward(self):
temp = self.head
while self.list:
num = self.list.pop(0)
node = ListNode(num)
temp.next = node
temp = node def readList(self,head):
temp =head
while temp.next != None:
temp = temp.next
print temp.val def Merge(self, pHead1, pHead2):
if not pHead1.next and not pHead2.next:
return
elif not pHead1.next:
return pHead2
elif not pHead2.next:
return pHead1
flag=0
temp=pHead1
temp1=temp.next
temp2=pHead2.next
while True: while temp1.val<temp2.val:
if temp1.next==None:
break
temp=temp1
temp1=temp.next node=temp2
if temp2.next == None:
flag=1
else:
temp2=temp2.next if temp1.next==None and temp1.val>node.val:
node.next = temp1
temp.next = node
temp = temp.next
elif temp1.next==None and temp1.val<node.val:
node.next=None
temp1.next=node
else:
node.next=temp1
temp.next=node
temp=temp.next
if flag==1:
break
return pHead1 s1=Solution([1,3,6,8,99,123])
s1.listCreateForward()
s2=Solution([2,3,5,7,11,13,15,16,18])
s2.listCreateForward()
s1.readList(s1.Merge(s1.head,s2.head))
链表的反转、合并(不借助额外list数组)的更多相关文章
- 理解单链表的反转(java实现)
要求很简单,输入一个链表,反转链表后,输出新链表的表头. 反转链表是有2种方法(递归法,遍历法)实现的,面试官最爱考察的算法无非是斐波那契数列和单链表反转,递归方法实现链表反转比较优雅,但是对于不 ...
- 02-线性结构1 两个有序链表序列的合并(15 point(s)) 【链表合并】
02-线性结构1 两个有序链表序列的合并(15 point(s)) 本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列. 函数接口定义: List Merge( List L ...
- 秒懂单链表及其反转(reverse)
什么是链表,这种数据结构是由一组Node组成的,这群Node一起表示了一个序列.链表是最普通,最简单的数据结构(物理地址不连续),它是实现其他数据结构如stack, queue等的基础. 链表比起数组 ...
- LeetCode初级算法--链表02:合并两个有序链表
LeetCode初级算法--链表02:合并两个有序链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...
- PTA 两个有序链表序列的合并
6-5 两个有序链表序列的合并 (15 分) 本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列. 函数接口定义: List Merge( List L1, List L ...
- Java实现单链表的反转
思路1:初始化一个新的头节点reverseHead,然后遍历旧链表,利用头插法向reverseHead进行插入 思路2: 1.反转相当于数据的更换(1和n,2和n-1,3和n-2)n为链表的长度 2. ...
- PHP合并2个数字键数组的值
先要了解一个基础知识点:PHP数组合并+与array_merge的区别分析 & 对多个数组合并去重技巧 <?php /** * PHP合并2个数字键数组的值 * * @param arr ...
- 将一个JSON数组[{},{},{}]按一定规则合并到另一个JSON数组[{},{},{}]
// 将一个JSON数组[{},{},{}]按一定规则合并到另一个JSON数组[{},{},{}] // Object.assign方法的第一个参数是目标对象,后面的参数都是源对象. var list ...
- 【loj6041】「雅礼集训 2017 Day7」事情的相似度 后缀自动机+STL-set+启发式合并+离线+扫描线+树状数组
题目描述 给你一个长度为 $n$ 的01串,$m$ 次询问,每次询问给出 $l$ .$r$ ,求从 $[l,r]$ 中选出两个不同的前缀的最长公共后缀长度的最大值. $n,m\le 10^5$ 题解 ...
随机推荐
- IntelliJ IDEA 2017版 spring-boot 2.03 去除控制台logo;去除springboot 图标;去除springboot 图
1.控制台出现logo图标 2.如果不喜欢,如何去除这个logo 在其Application中进行设置 package com.springboot.logo; import org.springfr ...
- es6 字符串方法
1.字符串的新方法 includes() 包含属性 startsWith() 头部开始是否包含 endWith() 字符串是否在尾部 ========三个返回值都为布尔值 第二参数为数字 e ...
- mysql学习之路_外键
回顾4 连接查询: 连接多张表到一起,不管记录数如何,字段数一定会增加. 分类:内连接,外连接.自然连接,交叉连接, 交叉连接:cross join (笛卡尔积) 内连接:inner join,左右两 ...
- 快速排序 JavaScript 实现
作为算法目录下的第一篇博文,快速排序那是再合适不过了.作为最基本最经典的算法之一,我觉得每个程序员都应该熟悉并且掌握它,而不是只会调用库函数,知其然而不知其所以然. 排序算法有10种左右(或许更多), ...
- 【2】C#读取文本文件
一.读取
- verilog中defparam的用法 (verilog调用底层模块(只改变)参数的传递)
当一个模块引用另外一个模块时,高层模块可以改变低层模块用parameter定义的参数值,改变低层模块的参数值可采用以下两种方式: 1)defparam 重定义参数 语法:defparam path_n ...
- day07_雷神_面向对象进阶
day07 1.接口类(抽象类) 接口类和抽象类是一种规范,写代码时的规范. 两个思想: 一个是统一接口,一个是定义规则. 最终版本:接口类,抽象类,是一种规范,写代码时的规范 强制性的规定. fro ...
- Opencv打开摄像头,读不到图像,一般来说先读取第一帧,舍弃,然后就正常了
舍弃第一帧的程序: cap >> img; cv::waitKey(100); if (cvWaitKey(5) == 27) break; cap >> img;
- Android-Java-synchronized同步代码块的使用场景
synchronized同步代码块的使用场景 (满足以下两种条件,就要考虑使用synchronize同步代码块了) 1.被synchronized同步代码块{同步的代码},是被多次异步调用,什么叫多次 ...
- MongoDB 无法创建抽象类的问题,
无法创建抽象类BsonClassMap.RegisterClassMap 大家都知道抽象类是无法实例化的,即:不能new. 在以下这些情况会遇到这种问题: 1.基类是抽象类: 2.基类是接口: 由于 ...