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
思路:
1.(头二个节点已经事先处理)找出要换位置的两个节点的前驱节点,设为tempHead,设要更换的两个节点为p,q
2.那么直接p节点拿出,tempHead->next=q;p->next=NULL;
3.然后就是普通把p几点插入到q节点后面即可。p->next=q->next;q->next=p;
4.最后把暂时头部节点后移两个位置,进行下一次更换。
图解如下:

代码:
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* res;
if(head==NULL) return NULL;
if(head->next==NULL) return head;
//交换第一,二个节点
ListNode* first=head->next;
head->next=first->next;
first->next=head;
ListNode* tempHead=first->next;
ListNode* p;
ListNode* q;
while (tempHead)
{
if(tempHead->next!=NULL&&tempHead->next->next!=NULL){
p=tempHead->next;
q=tempHead->next->next;
}
else return first;
tempHead->next=q;
p->next=NULL;//中间节点插入即可
p->next=q->next;
q->next=p;
tempHead=q->next;
}
return first;
}
};
Swap Nodes in Pairs(链表操作)的更多相关文章
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 24. Swap Nodes in Pairs 链表每2个点翻转一次
[抄题]: Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2 ...
- leetcode 24. Swap Nodes in Pairs(链表)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转
问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...
- 【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][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 ...
- 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 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
随机推荐
- 学JAVA第二十二天,StringBuffer的好处
五一的假期今天就结束了,又要回来上课了. 今天就写一下StringBuffer的好处吧. StringBuffer类的对象能够被多次的修改,并且不产生新的未使用对象. 也就是说,我们平时用String ...
- jq判断上下滚动
$(document).ready(function(){ var p=0,t=0; $(window).scroll(function(e){ p = $(this).scrollTop(); if ...
- 解决 图片在div中等比例缩放问题 (未解决:图片比例小于盒子模型时不会自动填充)
如题,该方案仅支持对图片等比例缩放.本文附件地址:https://files.cnblogs.com/files/john69-/background-Img.rar <!DOCTYPE htm ...
- vue-element:文件上传七牛之key和异步的问题
效果图: html 代码: <el-form-item label="Excel文件" :label-width="formLabelWidth" pro ...
- win7打开网络看不到局域网的其他电脑
双击打开桌面上的“网络”,在打开的窗口中看不到局域网的其他电脑/计算机.以前都可以看到的.可能是没有开启网络发现的原因,可是我并没有关闭网络发现.不知,怎么回事? Windows7查看网络邻居要开启g ...
- java实现的单点登录
摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统当中.本文从业务的角度分析了单点登录的需求和应用领域:从技术本身的角度分析了单点登录技术的内部机制和实现手段,并且给出Web-SSO ...
- 本地连接批处理修改IP
例子: 本地连接修改IP netsh interface ip delete dns "本地连接" addr=allnetsh interface ip add dns " ...
- 微信小程序中的图形验证码
可以在utils中新建一个mcaptcha.js 代码如下: module.exports = class Mcaptcha { constructor(options) { this.options ...
- ubuntu下pycharm无法使用pip安装python包的修复方案
1. 在pycharm 中安装python包会报错“pycharm ModuleNotFoundError: No module named 'distutils.core'”: 2. 可能原因:in ...
- Android(java)学习笔记202:JNI之hello.c(c代码功能实现)指针语法解析
1. 接下来我们细讲分析一下前面一讲中,c功能实现的代码: (1)hello.c : #include <jni.h> char* getHello() { //////// return ...