练习—单链表—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 ...
随机推荐
- 解决ie6显示透明图的问题
在我们设置png透明图片时,其他浏览器都显示很正常,唯独只有ie6看着不是透明的状态. 第一种办法是:单独设置ie6的样式.例: _background: none; _filter:progid:D ...
- Java学习笔记--对象克隆
转自:Edward_qing_Lee 的专栏 http://blog.csdn.net/edward_qing_lee/article/details/8249102 一.java 方法参数 理解: ...
- ububtu 彻底卸载程序的几种方法
sudo apt-get purge ......(点点为为程序名称) sudo apt-get autoremove sudo apt-get clean dpkg -l |grep ^rc|awk ...
- Cmake find_package()相关
也就是find_package可以帮助直接找到库的头文件和库文件(.lib,dll .etc) References: http://blog.csdn.net/dbzhang800/article/ ...
- Struts2标签库之iterator
传说中的第一种方式,这种在Struts2.1权威指南的例子中也木有说明白: <%@ page language="java" contentType="text/h ...
- Jetty直接调试,不用部署,不用弄一些杂七杂八的设置
以前调试web程序的,搭建Tomcat实在是费劲,就想找一个比较简单的方式,我就想调试一下我写的某一个servlet形式,看到<how Tomcat works>这本书,才明白确实可以,不 ...
- 页面onclick()中传值问题
html中onclick()里面传变量到javascript中的问题,小小的记录下: 传变量的话一定要加 '' <span onclick="sellGoods('${session ...
- [深入JUnit] 为什么别测试private函数
[深入JUnit] 为什么别测试private函数 摘自http://www.tuicool.com/articles/iumaayJ 时间 2016-03-28 10:58:03 SegmentFa ...
- c语言指针与结构体
#include <stdio.h> #include <stdlib.h> struct mydata { int num; ]; }; void main1() { /*i ...
- 今天弄了整整一天DCloud
开发一个新闻移动客户端 起先用appcan.apiCloud,最后选择了DCloud: MUI文档看了n久,js代码一上来不很适应编码风格,一堆括号弄得头大: 数据库由爬虫程序将新闻页面数据爬出来: ...