链表的基本操作:线性表 (单链表、循环链表-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数组)的更多相关文章

  1. 理解单链表的反转(java实现)

    要求很简单,输入一个链表,反转链表后,输出新链表的表头.   反转链表是有2种方法(递归法,遍历法)实现的,面试官最爱考察的算法无非是斐波那契数列和单链表反转,递归方法实现链表反转比较优雅,但是对于不 ...

  2. 02-线性结构1 两个有序链表序列的合并(15 point(s)) 【链表合并】

    02-线性结构1 两个有序链表序列的合并(15 point(s)) 本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列. 函数接口定义: List Merge( List L ...

  3. 秒懂单链表及其反转(reverse)

    什么是链表,这种数据结构是由一组Node组成的,这群Node一起表示了一个序列.链表是最普通,最简单的数据结构(物理地址不连续),它是实现其他数据结构如stack, queue等的基础. 链表比起数组 ...

  4. LeetCode初级算法--链表02:合并两个有序链表

    LeetCode初级算法--链表02:合并两个有序链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...

  5. PTA 两个有序链表序列的合并

    6-5 两个有序链表序列的合并 (15 分)   本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列. 函数接口定义: List Merge( List L1, List L ...

  6. Java实现单链表的反转

    思路1:初始化一个新的头节点reverseHead,然后遍历旧链表,利用头插法向reverseHead进行插入 思路2: 1.反转相当于数据的更换(1和n,2和n-1,3和n-2)n为链表的长度 2. ...

  7. PHP合并2个数字键数组的值

    先要了解一个基础知识点:PHP数组合并+与array_merge的区别分析 & 对多个数组合并去重技巧 <?php /** * PHP合并2个数字键数组的值 * * @param arr ...

  8. 将一个JSON数组[{},{},{}]按一定规则合并到另一个JSON数组[{},{},{}]

    // 将一个JSON数组[{},{},{}]按一定规则合并到另一个JSON数组[{},{},{}] // Object.assign方法的第一个参数是目标对象,后面的参数都是源对象. var list ...

  9. 【loj6041】「雅礼集训 2017 Day7」事情的相似度 后缀自动机+STL-set+启发式合并+离线+扫描线+树状数组

    题目描述 给你一个长度为 $n$ 的01串,$m$ 次询问,每次询问给出 $l$ .$r$ ,求从 $[l,r]$ 中选出两个不同的前缀的最长公共后缀长度的最大值. $n,m\le 10^5$ 题解 ...

随机推荐

  1. 【慕课网实战】Spark Streaming实时流处理项目实战笔记三之铭文升级版

    铭文一级: Flume概述Flume is a distributed, reliable, and available service for efficiently collecting(收集), ...

  2. 20155205 信息安全技术第二次实验 Windows口令破解

    20155205 信息安全技术第二次实验 Windows口令破解 实验原理 一.口令破解方法 口令破解主要有两种方法:字典破解和暴力破解. 字典破解是指通过破解者对管理员的了解,猜测其可能使用某些信息 ...

  3. java -help

    Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args... ...

  4. ORA-06553: PLS-553: character set name is not recognized, while starting Content Store

    Symptom CM-CFG-5029 Content Manager is unable to determine whether the content store is initialized. ...

  5. 用户权限,pymysql

    单表查询的完整语法 select [distinct] [*|字段|聚合函数|表达式] from tablewhere group byhaving distinctorder bylimit mys ...

  6. OkHttp3几个简单的例子和在子线程更新UI线程的方法

    okHttp用于android的http请求.据说很厉害,我们来一起尝尝鲜.但是使用okHttp也会有一些小坑,后面会讲到如何掉进坑里并爬出来. 首先需要了解一点,这里说的UI线程和主线程是一回事儿. ...

  7. noip第28课作业

    分段数列 [问题描述] 对于给定的一个长度为N的正整数数列A[i],现要将其分成连续的若干段,并且每段和不超过M(可以等于M),问最少能将其分成多少段使得满足要求. 输入格式: 输入第1行包含两个正整 ...

  8. 14:super关键字

    本小节知识点: 1.super基本概念 2.super的作用 1.super基本概念 super是个编译器的指令符号,只是告诉编译器在执行的时候,去调谁的方法. self是一个隐私参数; self r ...

  9. hdu 1.3.2 Moving Tables

    这道题比较简单,就是用数组存取其路径.如101和102是对门,其过道号可以记为51.  1和2之间的为1. 今早突然收到操作系统停课的通知,哈哈 回来就做一下水题,开心 #include<cst ...

  10. Windows 下环境变量设置(命令行)

    0. 背景 查了一下 SETX 命令可以实现,先简单看了下帮助.以后再写一个实例. 1. SETX /?    输出内容: SetX 有三种使用方式: 语法 1: SETX [/S system [/ ...