链表的反转、合并(不借助额外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$ 题解 ...
随机推荐
- location位置操作
使用location对象可以通过很多方式来改变浏览器的位置. location.assign('http://www.klkx.com') 传入一个URL地址 这样可以立即打开一个新的URL并在浏览器 ...
- Linux创建一个周期任务来定期删除过期的文件
一:需求 在开发中存在这样的情况,为了防止文件的误删,不允许开发人员直接删除项目中要用到的文件,而是将它们移动到某个目录,然后由一个周期任务去检测并删除内部过期的文件: 二:检测文件是否是过期文件 有 ...
- Win7 IIS 部署站点遇到的问题 如 HTTP 错误 404.XX
HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面. 解决办法:设置为允许-面相 =============== ...
- python爬虫 403 Forbidden 解决方法
模拟浏览器打开网页: headers={ 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, ...
- Sensor
原理:http://blog.csdn.net/xiaolei05/article/details/18670161 1.Sensor Type 重力感应/加速度传感器 (G-Sensor ...
- js-图片时间(倒计时)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- Visualise the Argyris basis functions
""" Author: kinnala Visualise the Argyris basis functions. """ from sk ...
- node-webkit学习(1)hello world
)hello world 文/玄魂 目录 node-webkit学习(1)hello world 前言 1.1 环境安装 1.1.1 windows下的安装 1.1.2 linux环境下的安装 1 ...
- 【VB.NET】利用 ZXing.Net 生成二维码(支持自定义LOGO)
有任何疑问请去我的新博客提出 https://blog.clso.fun/posts/2019-03-03/vb-net-zxing-net-qr-maker.html ZXing .NET 的项目主 ...
- Unity 环境区域网格化
在使用A星算法和物体布局的过程中,常常会使用的网格的概念,即建立在网格的基础上,会使得游戏的相关编程变得简单的多. 格子的代码: using System.Collections; using Sys ...