Swap Nodes in Pairs leetcode
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.
Subscribe to see which companies asked this question
ListNode* swapPairs(ListNode* head) {
if (head == nullptr || head->next == nullptr)
return head;
ListNode *pre = head;
ListNode *cur = pre->next;
ListNode *net = cur->next;
head = cur;
while (true)
{
cur->next = pre;
if (net == nullptr || net->next == nullptr) {
pre->next = net;
break;
}
pre->next = net->next;
pre = net;
cur = pre->next;
net = cur->next;
}
return head;
}
惊人的解法,用双重指针,开阔思路,本以为自己可以随心所欲使用双指针,看来还是差得远
ListNode *swapPairs(ListNode *head) {
ListNode **p = &head; while (*p && (*p)->next) {
ListNode *t = (*p)->next; (*p)->next = t->next;
t->next = *p;
*p = t; p = &(*p)->next->next;
} return head;
}
Swap Nodes in Pairs leetcode的更多相关文章
- Swap Nodes in Pairs——LeetCode
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- Swap Nodes in Pairs leetcode java
题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...
- Swap Nodes in Pairs LeetCode题解
做完这个题目,感觉LeetCode的题目出的真好... 这种题,如果让我在面试时候纸上写代码,肯定会挂的. 我昨天晚上看的题目,昨天脑子是懵的,放下了.今天早上来做. 一开始做,提交,果然错了.写的代 ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 【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 Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 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 ...
随机推荐
- 随机矩阵(stochastic matrix)
最近一个月来一直在看Google排序的核心算法---PageRank排序算法[1][2],在多篇论文中涉及到图论.马尔可夫链的相关性质说明与应用[3][4][5],而最为关键,一直让我迷惑 ...
- ASP.NET - 自定义控件处理页面事件(控件与页面数据交互)的方法
//用委托的方法实现 //控件代码 public delegate void DelegateFunction( string sPageTitle ); private DelegateFuncti ...
- Flex移动皮肤开发(一)
范例文件 mobile-skinning-part1.zip Flex 4.5提供的移动增强的皮肤特性,支持触摸交互.性能优良,并且考虑到了内存占用问题.尽管目前市场上有不少性能优异的设备,但典型的S ...
- bootstrap 按钮 文本 浮动 隐藏
bootstrap 按钮 文本 浮动 隐藏 <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- apache软件包下载地址
主地址: http://commons.apache.org/proper/commons-loggins/download_logging.cgi 镜像1: http://apache.fayea. ...
- java 调用webservice的各种方法总结,wsimport方法总结
http://www.blogjava.net/zjhiphop/archive/2009/04/29/webservice.html wsimport生成webservice客户端: wsimpor ...
- 在LINUX上创建GIT服务器
转载 如果使用git的人数较少,可以使用下面的步骤快速部署一个git服务器环境. 1. 生成 SSH 公钥 每个需要使用git服务器的工程师,自己需要生成一个ssh公钥进入自己的~/.ssh目录,看有 ...
- java7 invokedynamic命令深入研究
在看java虚拟机字节码执行引擎的时候,里面提到了java虚拟机里调用方法的字节码指令有5种: invokestatic //调用静态方法 invokespecial //调用私有方法.实例构造器方法 ...
- iOS 插件化开发汇总 Small框架
应用插件化背景 目前很多应用功能越来越多,软件显得越来越臃肿.因此插件化就成了很多软件发展的必经之路,比如支付宝这种平台级别的软件: 页上密密麻麻的功能,而且还在增多,照这个趋势发展下去,软件包的大小 ...
- C# OpenFileDialog 使用
OpenFileDialog ofd = new OpenFileDialog(); //设置标题 ofd.Title = "选择文件"; //是否保存上次打开文件的位置 ofd. ...