leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
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.
思路:题目比較简单,会链表反转的都能够做,思路也差点儿相同。不多说。上代码。
/**
* 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) { ListNode firstHead = new ListNode(0);
firstHead.next = head;
ListNode pre = firstHead;//定义一个头结点,这样全部的操作都同样
ListNode next = null;
while(head != null && head.next != null){
//A-B-C-D交换BC,pre=A;B=head;C=next
next = head.next;//保存交换的变量C head.next = next.next;//将B指向B的指针指向D
pre.next = next;//将A指向B的指针指向C
next.next = head;//将C指向D的指针指向B,完毕交换,顺序变为A-C-B-D //为下一循环准备变量
pre = head;//将pre变为B
head = head.next;//将head指向D }
return firstHead.next;
}
}
leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法的更多相关文章
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: 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. You may not modify the value ...
- LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- [leetcode]24. Swap Nodes in Pairs交换节点对
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- Leetcode 24——Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- (链表 递归) leetcode 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
- [LeetCode] 24. Swap Nodes in Pairs ☆
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
随机推荐
- 查看memcached依赖的库
LD_DEBUG=libs ./memcached -v
- javaTemplates-学习笔记一
第一步,下载Java运行环境-JDK JDK -'1.8.0_25'[附链接地址] 安装JDK配置环境 安装好jdk,然后配置环境变量.不同系统配置环境变量百度Google之,附上WIN8配置方法[J ...
- MySQL 表分区A
在MySQL中表的分区类型总的来说有四种: 第一种:range分区.基于一个给定的区间范围,把数据分配到不同的分区. 第二种:list 分区.基本枚举的值列表进行分区. 第三种:hast 分区.基 ...
- Toolkit 一键激活Office 2010方法及Office 2010 Toolkit工具下载
Office 2010激活的方法很多,但推荐使用本站亲测,可永久使用的两种方法,随便使用哪一种,一个不行就换另外一个.基本上只要Office 2010正常安装了,下面两种方法的任何一种都可以正常激活. ...
- Android SDK与API版本的对应关系
看教程.开发Android程序等很多地方,需要设置Android SDK的版本,而其要我们写的却是API版本的数字, 为了方便查看 Android SDK与API版本的对应关系 我在SDK Manag ...
- gallery 从最左边开始显示并且默认选中第一个
import android.content.Context; import android.graphics.Camera; import android.graphics.Matrix; impo ...
- jquery $.ajax $.get $.post的区别
$.ajax 是 jQuery 底层 AJAX 实现,$.ajax是一种通用的底层封装,$.ajax()请求数据之后,则需要使用回调函数,有beforeSend.error.dataFilter.su ...
- android程序启动画面之Splash总结[转]
方法一: 很多应用都会有一个启动界面.欢迎画面慢慢隐现,然后慢慢消隐.实现这种效果的方法有两种(暂时只发现两种)1.使用两个Activity,程序启动时候load第一张Activity,然后由tick ...
- Webform之(简单投票)练习
创建数据库: CREATE table DiaoYanTiMu ( Ids int primary key ,--题目代号 Title varchar() not null ,--要调查的题目 Sel ...
- table边框不显示
今日在做报表的时候发现,最后一行隐藏后整个报表的下边框会不显示,猜测是td的边框隐藏后但table并未设置边框,导致下边框没有出现.因此设置了table边框后问题解决.table和td的边框关系如下实 ...