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 ...
随机推荐
- BNU Online Judge-34777-Magical GCD
题目链接 http://www.bnuoj.com/bnuoj/problem_show.php?pid=34777 题意 如样例 输入 1530 60 20 20 20 输出 80 如 30 和 ...
- Spring4 Hibernate4 Struts2在WebLogic 10.3.3上面部署
折腾了4天,终于可以部署了: 1,lib下面的包: antlr-2.7.7.jarasm-5.0.2.jarasm-commons-5.0.2.jarasm-tree-5.0.2.jarc3p0-0. ...
- spring mvc 注解入门示例
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- nginx的配置服务器集群,负载均衡
在server{}前配置服务器ip和端口号 如: upstream local_tomcat { local_tomcat为访问路径,在下面配置服务器ip及端口号,也可以分配权重(weight==?) ...
- Bootstrap入门(十三)组件7:导航条
Bootstrap入门(十三)组件7:导航条 1.默认样式的导航条 2.嵌入表单和按钮 3.嵌入文本和非导航的链接 4.组件排列和下拉菜单 5.固定在顶部/底部 6.反色的导航条 7.路径导航 首先先 ...
- Javaweb阶段知识回顾一
java基础增强 一.jdk1.5的新特性 自动封箱拆箱 封箱:Java自动将原始类型值转换成对应的对象,如将int的变量转换成Integer对象 拆箱:自动将对应的对象转换成原始类型值,将Integ ...
- CentOS7 ssh无密码登录
准备工作:给各个主机取个名字,如master(主节点),slave01(从节点01),slave02(从节点02) 1.修改主机名: hostname master hostname slave01 ...
- PHP 7.1 新特性
PHP 7.1 新特性 1.密集阵算法 2.php int64位支持(2GB的字符串和2GB的文件的上传) 3.$a<=>$b 操作符,排序时有用 4.标量的支持,如果声明int传入st ...
- 关于j2ee工程发布到was上后,部分更新,例修改web.xml配置文件不起作用的原因解析【转】
在WAS中,应用的配置是从config/cells....目录下读取:而资源从/installedApps目录下读取 故当配置文件(例web.xml)发生改变时,只更新应用程序资源文件/install ...
- IBM面试记
link:http://kb.cnblogs.com/page/107213/ 话说其实我很久没有被正经面试过了.一开始去微软实习自然经过了经典的笔试和几轮面试,然后去了朋友的创业公司并立即被激动集团 ...