链表的反转、合并(不借助额外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$ 题解 ...
随机推荐
- mac环境下配置nginx
1.建议使用homebrew安装(ruby安装 brew install ruby) ruby -e "$(curl -fsSL https://raw.githubusercont ...
- ajax实现
AJAX是为了实现异步通信,提高用户体验度.JavaScript本身并不具有向服务器发送请求的功能(不使用NodeJs),要么使用window.open()方法重新打开一个页面向服务器发送请求,要么使 ...
- python爬虫之一:requests库
目录 安装requtests requests库的连接异常 HTTP协议 HTTP协议对资源的操作 requests库的7个主要方法 request方法 get方法 网络爬虫引发的问题 robots协 ...
- bzoj2879(动态加边费用流)
参考题解:http://blog.csdn.net/yxuanwkeith/article/details/52254602 //开始跑费用流用的dijkstra,一直错,后来发现动态加边后我不会处理 ...
- Leetcode--680. Valid Palindrome II(easy)
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- noip第29课作业
1. 钢条切割 [问题描述] 一家公司购买长钢条,将其切割成短钢条出售,切割本身没有成本,长度为i的短钢条的价格为Pi.那给定一段长度为n的钢条和一个价格表Pi,求钢条的切割方案使得收益Rn最大. ...
- Linux Shell学习笔记:exit退出状态代码
inux提供$?特殊变量来保存最后一条命令执行结束的退出状态.执行完一条命令后,立即执行echo$?,可以查看最后一条命令的退出状态值. 正常的情况下,命令成功执行完成的退出状态是0,如果非0,则命令 ...
- AngularJS 路由及SPA理解
一.路由及SPA理解 路由允许我们通过不同的 URL 访问不同的内容,可实现多视图的单页web应用(SPA);通常我们的URL形式为 http://runoob.com/first/page,但在单页 ...
- centos下添加epel源
RHEL以及他的衍生发行版如CentOS.Scientific Linux为了稳定,官方的rpm repository提供的rpm包往往是很滞后的,当然了,这样做这是无可厚非的,毕竟这是服务器版本,安 ...
- ZOJ 1259 Rails
stack的应用 #include<iostream> #include<cstdio> #include<stack> using namespace std; ...