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-& ... 
随机推荐
- justAP1.3.0版发布了
			justAP是一个简单的.易于使用的php运行环境,适合php开发人员使用.与wamp.xampp等不同,它仅仅包含Apache httpd和Php,这也是它名字的来由(justAP=just Apa ... 
- POJ 1823 Hotel 线段树
			题目链接 线段树的区间合并. 和上一题差不多....第三种操作只需要输出maxx[1]的值就可以. #include <iostream> #include <vector> ... 
- 【LeetCode题意分析&解答】37. Sudoku Solver
			Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ... 
- Xcode 那些简单实用的插件推荐
			古人云“工欲善其事必先利其器”,打造一个强大的开发环境,是立即提升自身战斗力的绝佳途径! 晾一下我的武器库,欢迎大家选用:) 全能搜索家CodePilot 2.0 ------------ ... 
- MySQL 设置数据库的隔离级别
			在会话级别设置隔离级别 1.read commited :set session transaction isolation level read committed; 2.repeatable re ... 
- finally块的问题(finally block does not complete normally) (转)
			当finall块中包含return语句时,Eclipse会给出警告“finally block does not complete normally”,原因分析如下: 1.不管try块.catch块中 ... 
- Android使用XML全攻略(2)
			Android使用XML全攻略(2) Android 是针对移动设备的一种新兴的开源操作系统和 SDK.借助它,您可以创建功能强大的移动应用程序.当您的应用程序可以访问 Web 服务时,其吸引力会 ... 
- LINQ to SQL的CRUD操作
			创建数据对象模型 sqlmetal /code:"C:\MyProjects\VS2008\Data\LinqConsoleApp2\LinqConsoleApp2\northwnd.cs& ... 
- [转载]HDFS的'Block'和MapReduce的'Split'之间的关系和区别
			http://www.cnblogs.com/xuxm2007/archive/2011/09/01/2162011.html hadoop的分块有两部分,其中第一部分更为人熟知一点. 第一部分就 ... 
- UVA1291----Dance Dance Revolution----3维DP
			本文出自:http://blog.csdn.net/dr5459 题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&a ... 
