【Leetcode】【Medium】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至少包含两个结点;
后条件:返回指向新链表第一个结点的指针;原链表结点全部加入新链表中,并且偶数对结点两两互换;新旧链表的结点数一致。
不变式:1、新建一个newhead指针,newhead->next永远指向新链表的头部;
2、新建一个newlist指针,newlist永远指向新链表的最后一个结点;
3、newlist->next始终为NULL;
4、head指针永远指向还未被操作的旧链表第一个结点;
5、新链表上的结点数加上旧链表剩余的结点数之和,应该和原链表结点数一致;
当head为空时,循环结束,每次循环:
1、将head中的结点按对取出~交换~插入newlist末端
2、若head只剩一个结点不够一对,则直接插入newlist末端;
解题步骤:
1、新建preHead结点,新建newlist指针;
2、按照不定式分析,开始循环,循环结束标志为head为空:
(1)如果当前head只剩一个结点,则将其插入newlist后,并结束循环;
(2)取出待操作的两个结点,head后移两位;
(3)将这两个结点反转插入newlist中
3、释放preHead,返回newlist;
代码1:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
/*
if (head == NULL || head->next == NULL)
return head;
*/
ListNode* newhead = new ListNode();
ListNode* newlist = newhead;
ListNode* preNode = NULL; while (head != NULL) {
if (head->next == NULL) {
newlist->next = head;
break;
}
preNode = head;
head = head->next->next;
preNode->next->next = preNode;
newlist->next = preNode->next;
newlist = preNode;
newlist->next = NULL;
} head = newhead->next;
delete newhead;
return head;
}
};
代码2,使用二维指针:
基本逻辑实现:
ListNode **p = &head;
while (*p && (*p)->next) {
// n表示待交换的两个结点中,后一个结点
ListNode* n = (*p)->next;
(*p)->next = n->next;
n->next = *p;
p = &(*p)->next;
}
由于二维指针p一直在操作当前需要交换的结点,不断向后迭代,而head指针此时指向的是链表中第二个结点(前两个交换);
因此上述代码唯一的问题是,没有指针指向头结点,无法返回...
所以我们希望在第一轮交换时,将head结点重新指向交换后的第一个结点。观察到第一轮交换时,*p其实就代表head,操作*p的指向,就是操作head的指向。
所以,最终代码为:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode *head) {
ListNode **p = &head; while (*p && (*p)->next) {
ListNode* n = (*p)->next;
(*p)->next = n->next;
n->next = *p;
*p = n;
p = &(*p)->next->next;
} return head;
}
};
【Leetcode】【Medium】Swap Nodes in Pairs的更多相关文章
- 【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 ...
- 【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 【 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题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 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 ...
随机推荐
- c# xml序列化和反序列化。也就是xml的解析和反解析。
用习惯了newTownSoft.json 的json反序列化.碰到xml是真的不习惯. 每次json反序列化都是直接把json丢到bejson网站生成一个实体类,稍微修改修改一点点变量名.然后直接ne ...
- oracle 表空间tablespace
一.Oracle 表空间的组成 Everoone knows Oracle数据库真正存放数据的是数据文件,Oracle表空间是逻辑上的概念,他在物理上是并不存在的,把多个DataFile合并到一起就是 ...
- Robot Framework自动化测试一(第一个脚本)
创建测试项目 选择菜单栏file----->new Project Name 输入项目名称,Type 选择Di ...
- 012-MD5Utils工具类模板
package ${enclosing_package}; import java.math.BigInteger; import java.security.MessageDigest; impor ...
- linux 查看端口,开启新端口
一.查看端口被占用命令 1.lsof -i:端口号 2.netstat -tunlp|grep 端口号 3.netstat -anp 查看哪些端口被打开 上面命令是查看端口被进程占用的情况 二.开启新 ...
- MySql的内置函数
MySQL的内置函数不但可以在SELECT查询语句中应用,同样也可以在INSERT.UPDATE和DELECT等语句中应用.例如,在INSERT添加语句中,应用日期时间函数获取系统的当前时间,并且将其 ...
- Hibernate中的CRUD操作
1.添加数据操作 插入数据使用session对象的save()方法完成. 插入代码: @Test public void Test1(){ SessionFactory sessionFactor ...
- ASP.NET Core中使用自定义路由
上一篇文章<ASP.NET Core中使用默认MVC路由>提到了如何使用默认的MVC路由配置,通过这个配置,我们就可以把请求路由到Controller和Action,通常情况下我们使用默认 ...
- 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
@RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...
- Lucene学习之四:Lucene的索引文件格式(1)
本文转载自:http://www.cnblogs.com/forfuture1978/archive/2009/12/14/1623597.html Lucene的索引里面存了些什么,如何存放的,也即 ...