21. Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
head = ListNode(0)
cur = head
while l1 and l2:
if l1.val <= l2.val:
cur.next = l1
l1 = l1.next
else:
cur.next = l2
l2 = l2.next
cur = cur.next
cur.next = l1 or l2
return head.next
# iteratively

141. Linked List Cycle

 

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
while head.next is not None:
if head.value is None:
return True
head.value = None
head = head.next
return Faulse

LeetCode with Python -> Linked List的更多相关文章

  1. LeetCode专题-Python实现之第21题:Merge Two Sorted Lists

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  2. [LeetCode] 92. Reverse Linked List II_Medium tag: Linked List

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  3. [LeetCode] 92. Reverse Linked List II 反向链表II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  4. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  5. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  6. LeetCode专题-Python实现之第27题:Remove Element

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  8. LeetCode专题-Python实现之第20题:Valid Parentheses

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  9. LeetCode专题-Python实现之第9题:Palindrome Number

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

随机推荐

  1. 流媒体 5——MPEG声音

    1. 听觉系统的感知特性: MPEG声音的数据压缩和编码不是依据波形本身的相关性和模拟人的发音器官的特性,而是利用人的听觉系统的特性来达到压缩声音数据的目的,这种压缩编码称为感知声音编码. 许多科学工 ...

  2. Django Form 表单

    Form 表单功能 生成HTML表单元素检查表单元素的合法性验证如果错误,重复显示表单数据类型转换 Form相关的对象 Widget 渲染成HTML元素的工具Field Form对象中的一个字段For ...

  3. IOS UIImageView的帧动画

    ● UIImageView可以让一系列的图片在特定的时间内按顺序显示 ● 相关属性解析: ● animationImages:要显示的图片(一个装着UIImage的NSArray) ● animati ...

  4. postman传递参数的问题

    postman是一款通过post或者get发送请求测试代码的工具 如果是类的话,就选择JSON格式,如果是一个字段的方法,就直接写入方法值就好了比如 public PageResult<Info ...

  5. UVA 12169 Disgruntled Judge(Extended_Euclid)

    用扩展欧几里德Extended_Euclid解线性模方程,思路在注释里面了. 注意数据范围不要爆int了. /********************************************* ...

  6. Android(java)学习笔记94: SurfaceView使用

    1. SurfaceView简介    在一般的情况下,应用程序的View都是在相同的GUI线程(UI主线程)中绘制的.这个主应用程序线程同时也用来处理所有的用户交互(例如,按钮单击或者文本输入). ...

  7. 【转】iOS 文件下载及断点续传

    ios的下载我们可以使用的方法有:NSData.NSURLConnection.NSURLSession还有第三方框架AFNetworking和ASI 利用NSData方法和NSURLConnecti ...

  8. python_62_装饰器5

    import time def timer(func): #timer(test1) func=test1 def deco(*args,**kwargs): start_time=time.time ...

  9. KVM修改虚机网卡模式:由NAT模式改为Bridge模式

    1)关闭虚机# virsh  shutdown  vm1 2)编辑虚机配置文件# virsh  edit  vm1 <interface type='default'> 改为<int ...

  10. GIT 团队协作快速入门使用

    GIT使用: 1.本地新建一个文件夹 git init 2.克隆远程仓库 git clone git@xxxxx.git 3.本地创建一个dev分支 (前提是服务器端已经创建好有 DEV 分支) gi ...