【leetcode❤python】24. Swap Nodes in Pairs
#-*- coding: UTF-8 -*-
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
dummy=ListNode(0)
dummy.next=head
pre,cur=dummy,head
while cur and cur.next: #cur=1,cur.next=2
pre.next=cur.next #dummy--->2
cur.next=cur.next.next #1-->3
pre.next.next=cur #2-->1
pre,cur=cur,cur.next #pre=1,cur=3
return dummy.next
【leetcode❤python】24. Swap Nodes in Pairs的更多相关文章
- 【LeetCode】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- 【LeetCode】24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- leetcode个人题解——#24 Swap Nodes in Pairs
因为不太熟悉链表操作,所以解决方法烦了点,空间时间多有冗余. 代码中l,r分别是每一组的需要交换的左右指针,temp是下一组的头指针,用于交换后链接:res是交换后的l指针,用于本组交换后尾指针在下一 ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- Leetcode 题目整理-6 Swap Nodes in Pairs & Remove Duplicates from Sorted Array
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exa ...
随机推荐
- Java IO总结之缓冲读入文件
package com.io; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException ...
- [sinatra] Sinatra再入门
原文URL:http://www.rubycc.com/bbs/topic_detail/86 1.基础代码app.rb require 'rubygems' require 'sinatra/bas ...
- 【pyQuery分析实例】分析体育网冠军联盟比赛成绩
目标地址:http://www.espncricinfo.com/champions-league-twenty20-2012/engine/match/574265.html liz@nb-liz: ...
- Yii框架,在页面输出执行sql语句,方便调试
1.下载yiidebugtb,并且放入到 application.extensions.yiidebugtb 目录 2.修改main.php,加入如下代码: 'log'=>array( 'cla ...
- some software that is used to speed up your system
1.RAMDISK take some space in ram and use them as the disk. Primo Ramdisk Server Edition 5.6.0 regist ...
- Fragement
package com.exmple.frage; import java.util.ArrayList; import java.util.Calendar; import java.util.Ha ...
- IT书籍的选择与阅读
拍摄是一件艰苦而有趣的事情. 它需要眼睛和心灵完全融合投入, 需要耐心等待排除干扰, 需要敏捷捕捉稍纵即逝的瞬间. 但是, 非艰苦不成乐趣. 人生也应该选择一件有难度的事情来做. 做不成, 可以收获过 ...
- ubuntu硬件配置查看命令
主板:sudo dmidecode |grep -A16 "System Information$"
- 输入框焦点时自动清除value
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <script typ ...
- SQL中char、varchar、nvarchar的区别
char char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符. nvarcha ...