练习—单链表—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++安装JSONCPP
VS2013里新建一个空的控制台程序(用作测试jsoncpp是否可用),名为: TestJSON 解压下载好的文件:jsoncpp-src-0.5.0.tar.gz 利用VS2008打开jsoncpp ...
- Win7下部署 .NET MVC网站 之 HTTP错误 403.14-Forbidden 解决方法
今天在 IIS 7 发布MVC 站点时 遇到 ”HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容 “ 的错误提示. 一番折腾后发现在web.config 中加入 ...
- WPF、WinForm(C#)多线程编程并更新界面(UI)(转载积累)
using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using ...
- NodeJs简单七行爬虫--爬取自己Qzone的说说并存入数据库
没有那么难的,嘿嘿,说起来呢其实挺简单的,或者不能叫爬虫,只需要将自己的数据加载到程序里再进行解析就可以了,如果说你的Qzone是向所有人开放的,那么就有一个JSONP的接口,这么说来就简单了,也就不 ...
- AngularJS自定义表单控件
<!doctype html> <html ng-app="myApp"> <head> <script src="G:\\So ...
- IOC容器MEF在MVC中的使用
最近想把自己的网站框架用IOC改造下,经过对比,我初步选择autofac,虽然MEF不需要配置,但性能不行,autofac虽然需要自己写自动化注入,但性能非常好. 先分析下各大IOC框架的性能,分两类 ...
- Spring security oauth2最简单入门环境搭建
关于OAuth2的一些简介,见我的上篇blog:http://wwwcomy.iteye.com/blog/2229889 PS:貌似内容太水直接被鹳狸猿干沉.. 友情提示 学习曲线:spring+s ...
- windows mongodb 安装
window平台一下所有命令 务必以管理员身份运行 且在window系统命令行下,如git bash不可以 具体以管理员身份运行的快捷键是 1. win+x 2. shift + a 设置文件存储目录 ...
- PSAM读卡芯片TDA8007BHL开发
WWT:Work Waiting Time ATR:Answer To Reset,复位应答 etu =F/Df 1. PSAM概述和应用 PSAM(PurchaseSecure Access ...
- 二极管IN4001~IN4007参数
电压范围50~1000V 正向导通电流1A 导通电压降:1.1V 具体见下图: