练习—单链表—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.
改变指针:
if (!head || !(head->next) ) return head; ListNode *cur = head,*next = head->next; head = head->next; while(next){ next = next->next;//next->3 cur->next->next =cur;//2->1 if(!next || !(next->next)) { cur->next = next;//1->3; return head; } cur->next = next->next;//否则 1->4 cur = next;// cur ->3 next = next->next;//next->4 } return head;
只改变值:
ListNode* swapPairs(ListNode* head) { if(!head) return nullptr; ListNode *frontPtr=head,*backPtr=head->next; while(frontPtr && backPtr){ swap(frontPtr->val,backPtr->val); frontPtr=frontPtr->next; if(frontPtr!=nullptr) frontPtr=frontPtr->next; backPtr=backPtr->next; if(backPtr!=nullptr) backPtr=backPtr->next; } return head; }
递归:
class Solution { public: ListNode* swapPairs(ListNode* head) { if(head == NULL||head->next==NULL) return head; ListNode* next = head->next; head->next = swapPairs(next->next); next->next = head; return next; } };
练习—单链表—Swap Nodes in Pairs的更多相关文章
- 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 <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- [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 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
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
- leetcode-algorithms-24 Swap Nodes in Pairs
leetcode-algorithms-24 Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and re ...
- LeetCode: Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
随机推荐
- c++中vector等容器的实现机制
stl容器区别: vector list deque set map-底层实现 stl容器区别: vector list deque set map (转) 在STL中基本容器有: vector.li ...
- 你好,C++(19)“老师,我这次四级考试过了没有?”——4.2 条件选择语句
4.2 条件选择语句 “老师,我这次四级考试过了没有?” 如果老师被问到这个问题,他会如何回答?是的,他会根据不同的条件选择不同的回答: 如果考试成绩大于等于60,那就回答:“恭喜你,你通过了这次考 ...
- Spring 学习笔记01
以一个论坛登陆模块来讲解如何使用spring 登陆功能虽然简单,但是模块虽然很小,但是基本包括了一般的web应用的操作.涵盖了持久层数据访问(数据库相关操作).业务层事务管理(数据库操作回滚等).展现 ...
- 《APUE》读书笔记第十二章-线程控制
本章中,主要是介绍控制线程行为方面的内容,同时介绍了在同一进程中的多个线程之间如何保持数据的私有性以及基于进程的系统调用如何与线程进行交互. 一.线程属性 我们在创建线程的时候可以通过修改pthrea ...
- 一个Hadoop难以查找的错误
This script is Deprecated. Instead use start-dfs.sh and start-yarn.sh Starting namenodes on [Master1 ...
- USB 调试工具(python2.7 + Tkinter + pyusb/pywinusb)
项目地址:USB-HID-TEST 整体预览图(win8下的效果): ====================== 项目结构: COM --hidHelper.py --usbHelper.py UI ...
- (2) 假设字符串类似这样的aba和aab就相等,现在随便给你二组字符串,请编程比较他们看是否相等
/** * 第一种方式: * 实现思路:将字符串通过getBytes方法转换为byte数组,或者通过toCharArray()转换为char数组 * 然后先调用Arrays的sort方法进行排序,再调 ...
- struct2(四)编写Struct2 的Action
简介: 1.映射一个Action到一个类上面 2.把结果返回到view展示 3.编写Action对应的控制逻辑 1. Action Mapping <action name="he ...
- hdu3415:最大k子段和,单调队列
题目大意:给定长度为n的数组,求出最大的区间和,其中区间长度在[1,k]之间 分析: 学动态规划的时候我们会遇到一个经典问题 最大子段和,这个题跟最大子段和很类似 不同的是区间的长度有限制,无法用原算 ...
- centos无线网卡设置
联想thinkpad E430 ,安装上centos后,无线网卡一直无法使用,经历了艰苦曲折到网卡驱动安装过程,历时2个晚上终于开心到用上无线网卡. 1.查看无线网卡到具体型号 [root@local ...