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 ...
随机推荐
- SSH网上商城---商品详情页的制作
在前面的博文中,小编分别简单的介绍了邮件的发送以及邮件的激活,逛淘宝的小伙伴都有这样的体会,比如在搜索框中输入连衣裙这个商品的时候,会出现多种多样各种款式的连衣裙,连衣裙的信息包括价格,多少人购买,商 ...
- TCP的发送系列 — 发送缓存的管理(二)
主要内容:从TCP层面判断发送缓存的申请是否合法,进程因缺少发送缓存而进行睡眠等待. 因为有发送缓存可写事件而被唤醒. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zh ...
- 【并发编程】ThreadPoolExecutor参数详解
ThreadPoolExecutor executor = new ThreadPoolExecutor( int corePoolSize, int maximumPoolSize, long ke ...
- SSH网上商城---需求分析+表关系分析
SSH---小编初次接触的时候傻傻的以为这个跟SHE有什么关系呢?又是哪路明星歌手,后来才知道小编又土鳖了,原来SSH是这个样子滴,百度百科对她这样阐述,SSH即 Spring + Struts +H ...
- JPA(三)之实体关系一对多(多对一)
1.背景介绍: 对于购买商品时,订单信息(Order)和订单商品信息(OrderItem)的关系就是一对多的关系. 2.实体bean: Order.java代码 ? 1 2 3 4 5 6 7 ...
- 【嵌入式开发】ARM 芯片简介 (ARM芯片类型 | ARM处理器工作模式 | ARM 寄存器 | ARM 寻址)
: 12MHz 晶振 对应 405 ~ 532 MHz 处理速度; -- : 16K 指令缓存, 16K 数据缓存; -- : 32KB 指令缓存, 32KB 数据缓存; (3) 内存接口对比 : 提 ...
- 推荐一本不错的书《Sencha Ext JS 5 Bootcamp in a Book》
原文:https://www.createspace.com/5425618 看了一下该书目录,感觉不错,Ext JS 5的重点内容都提及了,确实是一本学习Ext JS 5的好书,唯一遗憾的地方就是太 ...
- VB.NET版机房收费系统---组合查询
查询的意思就是查找,寻找,指在某一个或几个地方找出自己所要的信息,假如我想搜索一下我自己写的博客,名字叫做初雪之恋,我在百度的搜索框中输入丁国华三个字,会有怎样的惊喜等着我? 啊哦,这个信息并不是我想 ...
- ant+eclipse知识点详解及使用案例
ant的优点和地位就不再阐述,下面直接上知识点: 在java中使用xml文件开发,有以下基本语法 (1)project:每个ant程序有且只有一个此标签,而且是类似于html的总标签,有name,de ...
- MinerUtil.java 爬虫工具类
MinerUtil.java 爬虫工具类 package com.iteye.injavawetrust.miner; import java.io.File; import java.io.File ...