LeetCode之旅(21)-Swap Nodes in Pairs
题目:
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.
思路:
- 首先是判断head和head.next为空的情况,直接返回head
- 然后设置first和second两个变量,用来记录一前一后两个元素
- 设置pre来记录上一次最后的元素。pre.next = second;
-
代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null||head.next == null){
return head;
}
ListNode first = head;
ListNode second = head.next;
ListNode pre = null;
ListNode swap = null;
if(second != null){
head = second;
}
while(first != null&&second != null){
swap = second.next;
second.next = first;
first.next = swap;
if(pre != null){
pre.next = second;
}
pre = first;
first = swap;
if(swap != null){
second = swap.next;
}
}
return head;
}
}
LeetCode之旅(21)-Swap Nodes in Pairs的更多相关文章
- leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现
题目: 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第23题--Swap Nodes in Pairs
Problem: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1 ...
- LeetCode(24) Swap Nodes in Pairs
题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...
- 【leetcode❤python】24. Swap Nodes in Pairs
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- 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 ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
随机推荐
- Description Resource Path Location Type AndroidManifest.xml file missing!
这个问题又找了好久.国内回答的确不敢恭维. 本回答来自谷歌: This is build issue. Go to Menu in eclipse, Project>clean then P ...
- SSH深度历险(五) 深入浅出-----IOC AND AOP
IOC就是Inversion of Control,控制反转.在Java开发中,IoC意味着将你设计好的类交给系统(容器)来控制实现,而不是在你的类内部控制.这称为控制反转. 本人理解:就是把原本你自 ...
- Android初级教程XUtils实现“断点续传”下载
对于"断电续传",在任何开发中都显得很重要.xutils对此封装的很好了,可以很简单的实现很多下载功能,其中就包括"断点续传" 主要代码如下: package ...
- (七十四)iOS8之前使socket可以后台运行的方法
对于使用socket通信的应用程序,常常希望App位于后台时仍然可以进行网络通信,这在iOS8和以后的版本是被默认允许的,socket可以直接在后台运行,而对于iOS8之前的版本就不行,需要进行两步设 ...
- IOC 控制反转(Inversion of Control,英文缩写为IoC)
在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 在这样的齿轮组中,因为是协同工作,如果有一个齿轮出了问题,就可能会影响到整个齿 ...
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十七)
现在玩家选择机器人后,可以在屏幕上或手臂上点击来移动robot's arm了. 但是玩家选择一个机器人后没有视觉效果来表明哪个机器人被选中.玩家做了一个操作后没有视觉反馈会惹恼强迫症用户滴 ;) 这篇 ...
- 尚学堂马士兵struts2 课堂笔记(三)
19-20 简单数据验证 例如 前台jsp 及struts.xml <a href="user/user!add?name=a" >添加用户</a> < ...
- 有关Spring注解@xxx的零碎知识
在Java的Spring开发中经常使用一些注解,例如 @XXX 等等,在网上看到收集整理碎片知识,便于懒人计划^=^... 过去,Spring使用的Java Bean对象必须在配置文件[一般为a ...
- outlook邮箱邮件与企业邮箱同步(outlook本地文件夹邮件,web邮箱里没有)
用惯了outlook2010, 问题:今天将邮件放到自定义文件夹后,发现在web邮箱中看不到邮件了.不能同步到企业邮箱. 解决忙了一天,才知道是账户类型问题,pop3类型,只下载不上传.所以outlo ...
- 类装载器DexClassLoader (android内核剖析)
在java环境中,有个概念叫做"类装载器",其作用是动态装载Class文件.标准的java SDK中有一个ClassLoader类,借助它可以装载 想要的Class文件,每个Cla ...