【LeetCode with Python】 Swap Nodes in Pairs
来源:互联网 发布:阿里云实名认证 编辑:程序博客网 时间:2024/05/29 19:49
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/swap-nodes-in-pairs/
题目类型:链表
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/3465506
交换单链表中每一对相邻的奇偶数下标的元素,注意防止链表长度可能是奇数,即不要忘记检查second_node是否为None。
原题页面:https://oj.leetcode.com/problems/swap-nodes-in-pairs/
题目类型:链表
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/3465506
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4
, you should return the list as 2->1->4->3
.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
交换单链表中每一对相邻的奇偶数下标的元素,注意防止链表长度可能是奇数,即不要忘记检查second_node是否为None。
class Solution: # @param a ListNode # @return a ListNode def swapPairs(self, head): if None == head: return head new_head = ListNode(0) new_head.next = head last_node = new_head while True: first_node = last_node.next if None == first_node: break second_node = first_node.next if None == second_node: break last_node.next = second_node first_node.next = second_node.next second_node.next = first_node last_node = first_node return new_head.next
- 【LeetCode with Python】 Swap Nodes in Pairs
- Swap Nodes in Pairs Leetcode Python
- 24. Swap Nodes in Pairs Leetcode Python
- [leetcode: Python]24. Swap Nodes in Pairs
- 【Leetcode】【python】Swap Nodes in Pairs
- LeetCode:Swap Nodes in Pairs
- LeetCode: Swap Nodes in Pairs
- [Leetcode] Swap Nodes in Pairs
- LeetCode : Swap Nodes in Pairs
- [LeetCode] Swap Nodes in Pairs
- Leetcode: Swap nodes in pairs
- [LeetCode]Swap Nodes in Pairs
- [LeetCode]Swap Nodes in Pairs
- LeetCode-Swap Nodes in Pairs
- LeetCode - Swap Nodes in Pairs
- LeetCode | Swap Nodes in Pairs
- 【leetcode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- -
- 图解VirtualBox 的共享文件夹设置
- C#中泛型特性2-约束
- -
- 迪堡ATM Opteva 系列安装验机条系列文章
- 【LeetCode with Python】 Swap Nodes in Pairs
- 【LeetCode with Python】 Validate Binary Search Tree
- 【LeetCode with Python】 String to Integer (atoi)
- 【LeetCode with Python】 Single Number
- apache 2 安装
- 【LeetCode with Python】 Same Tree
- C#教学第14讲类型转换2(学习笔记)
- 【LeetCode with Python】 Reverse Words in a String
- unix多线程,互斥锁和信号灯