【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
参考资料 https://www.renren.io/guide/
- Django跨域解决方法
from django.utils.deprecation import MiddlewareMixin class Mymiddle(MiddlewareMixin): def process_re ...
- SpringBoot1.5.2安装配置--1.5.2版本问题
简述下java环境 1.安装jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...
- StringUtils 中 isEmpty 和 isBlank 的区别
在项目的工作学习中经常用到了 apache commons 中的 StringUtils 的 isBlank 和 isEmpty 来判断字符串是否为空,这个方法都是判断字符串是否为空做判断的,以至于 ...
- 网络安装Ubuntu16.04
网络安装Ubuntu16.04 搭建PXE服务器 PXE是Pre-boot Execution Environment,预启动执行环境.是通过网络安装任何linux系统最重要的步骤. 首选搭建PXE服 ...
- 鹅厂优文|主播pk,如何实现无缝切换?
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文作者,rexchang(常青),腾讯视频云终端技术总监,2008 年毕业加入腾讯,一直从事客户端研发相关工作,先后参与过 PC QQ.手 ...
- python3根据地址批量获取百度地图经纬度
python3代码如下: import requests import time def get_mercator(addr): url= 'http://api.map.baidu.com/geoc ...
- h5页面ios,双击向上滑动,拖拽到底部还能继续拖拽(露出黑色背景)
h5页面ios,双击向上滑动,拖拽到底部还能继续拖拽 标签: 手机 2016-02-02 18:09 696人阅读 评论(0) 收藏 举报 在ios下,双击屏幕某些地方,滚动条会自动向上走一段. ...
- jQuery ajax async
jQuery 同步调用: jQuery.ajax({ type:'POST', async: false, url:'qcTask/add', contentType:'application/jso ...
- node版本的切换(转)
大量开发者的贡献使Node版本的迭代速度很快,版本很多(横跨0.6到0.11),所以升级Node版本就成为了一个问题.目前有n和nvm这两个工具可以对Node进行无痛升级,本文简单介绍一下二者的使用. ...