【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 ...
随机推荐
- XNA项目基础
XNA项目基础 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Frame ...
- JavaScript 刷题一
最近读<JavaScirpt编程精解>,想把里面的三个大的程序实现,现在记录下来. 问题一: 从下面这封信中,emily奶奶每封信的结尾都会用同样的格式注明哪只猫出生了,哪只猫死去了.现要 ...
- MySQL数据表的修改
数据表的修改包括列的增加.列的删除.约束的添加.约束的删除等. 添加单列 ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [F ...
- Robot Framework的安装、更新与卸载
Robot Framework的安装.更新与卸载 一,安装RF前的准备 一般就三种执行环境 Python, Jython (JVM) 和 IronPython (.NET): 安装python: #T ...
- mac下 mysql 插入中文乱码解决
搞了好几个小时,终于搞定了 乱码根本原因还是编码方式不同造成的,只要编码方式统一就没问题 1. 进入 mysql 命令行, 用 show variables like 'character_set_% ...
- 关于echar彩色柱状图颜色配置问题
在做配置页面的时候,有这样一个需求,用户可以选择配置是单色柱状图或者彩色的柱状图,并且颜色可以自定义的 思路:首先默认构建一个一个空的图表,然后根据记录上一次操作保存过来的参数重新组建option的值 ...
- Linux 信号signal处理函数--转
alarm(设置信号传送闹钟)相关函数 signal,sleep 表头文件 #include<unistd.h> 定义函数 unsigned int alarm(unsigned int ...
- Unity3d编辑器扩展学习笔记
编辑器扩展 1.添加菜单栏:把特性应用于静态方法 参数1:菜单名的空格后面是定义快捷键(单符号得用"_"开头,组合键%=Ctrl,#=Shift,&=Alt) 参数2:通过 ...
- Ms SQL Server 游标嵌套 初始化数据
--TRUNCATE TABLE TAB_ROLE_FUNC; --SELECT * FROM TAB_ROLE_FUNC; ), ; --外层游标 DECLARE CURSOR_ROLE CURSO ...
- 100个实用的CSS技巧,以及遇见的问题和解决方案。
前言 本篇文章将会持续更新,我会将我遇见的一些问题,和我看到的实用的CSS技巧记录下来,本篇文章会以图文混排的方式写下去.具体什么时候写完我也不清楚,反正我的目标是写100个. 本案例都是经本人实测 ...