【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 ...
随机推荐
- springboot pom.xml文件
pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- oracle 表空间tablespace
一.Oracle 表空间的组成 Everoone knows Oracle数据库真正存放数据的是数据文件,Oracle表空间是逻辑上的概念,他在物理上是并不存在的,把多个DataFile合并到一起就是 ...
- LeetCode 100.相同的树(C++)
给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1 ...
- 权重平等分布局And TableRow布局误区
开头语: 本人最近在自学Android,虽然本人有2年Java Web的开发经验.但是发现Android的自学之路并不是那么平坦,我没有Android真机.但是有一个window phone的手机.开 ...
- 【wordpress】wordpress环境的搭建
WordPress WordPress 是一种使用 PHP语言和 MySQL数据库开发的开源.免费的Blog(博客,网志)引擎,用户可以在支持 PHP 和 MySQL 数据库的服务器上建立自己的 Bl ...
- django允许跨域请求配置
django允许跨域请求配置 下载corsheader pip install django-cors-headers 修改setting.py中配置 在INSTALLED_APPS中增加corshe ...
- Python基础(5) - 文件
Python Python提供的函数和方法方便地对文件进行读.写.删除等的操作. open()函数返回一个文件对象. open(name[, mode[, buffering]]) -> fil ...
- [Hadoop大数据]--kafka入门
问题导读: 1.zookeeper在kafka的作用是什么? 2.kafka中几乎不允许对消息进行“随机读写”的原因是什么? 3.kafka集群consumer和producer状态信息是如何保存的? ...
- linq中where与skipwhile区别
//字符串数组 string[] names = { "a1", "a2", "bcd","ab","bcde ...
- PHP读取配置文件连接MySQL数据库
读取配置文件方法parse_ini_file($filepath [,$section]) 代码: conn.php <?php //连接数据库 //$conn =new mysqli('loc ...