【LeetCode】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.
code :
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(head == NULL)
return NULL;
if(head->next == NULL)
return head; ListNode *p = head;
ListNode *q = head->next;
ListNode *pre = NULL;
while( q != NULL && q->next != NULL) // p,q 指向相邻结点一前一后
{ // 注意奇数个结点的情况,判空为第一种
ListNode *tmp = q->next;
q->next = p;
p->next = tmp;
if( pre == NULL)
{
head = q;
pre = p;
}
else
{
pre->next = q;
pre = p;
}
p = p->next;
q = p->next;
}
if(pre == NULL) //只有两个结点
{
head = q;
q->next = p;
p->next = NULL;
return head;
}
if(q == NULL) //奇数个结点
{
return head;
}
q->next = p; //偶数个结点
p->next = NULL;
pre->next = q;
return head; }
};
【LeetCode】Swap Nodes in Pairs的更多相关文章
- 【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 [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- 【leetcode】Swap Nodes in Pairs (middle)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【LeetCode】Swap Nodes in Pairs(两两交换链表中的节点)
这是LeetCode里的第24题. 题目要求: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定1->2->3->4, 你应该返回2->1->4- ...
- 【Leetcode】【Medium】Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【链表】Swap Nodes in Pairs(三指针)
题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 【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 【 Linked List Swap Nodes in Pairs 】 python 实现
题目: Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
随机推荐
- Qt Quick 与 QML语言(初学笔记1)
Qt Quick Qt Quick是一些新的UI技术的集合,用来帮助开发者创建一种现在越来越多用于手机.多媒体播放器.机顶盒以及其他便携式设备上的直观的.现代的.流畅的用户界面.简单来说,Qt Qui ...
- Jquery实现图片左右滚动(自动)
<!DOCTYPE HTML><html><head><title>基于jQuery的控制左右滚动效果_自动滚动版本</title>< ...
- css样式积累
1.圆角: border-radius:16px 16px 16px 16px; 2透明度: filter: alpha(opacity=80); ...
- each函数循环数据表示列举,列举循环的时候添加dom的方法
var dotBox = $('#bannerNum');var item = '<li></li>';var itemSize = $('#bannerBack p').le ...
- SQL 结构化查询语言
SQL 结构化查询语言 一.数据库的必要性: >>作用:存储数据.检索数据.生成新的数据 1)可以有效结构化存储大量的数据信息,方便用户进行有效的检索和访问. 2)可以有效地保持数据信息的 ...
- 教你在Java的普通类中轻松获取Session以及request中保存的值
曾经有多少人因为不知如何在业务类中获取自己在Action或页面上保存在Session中值,当然也包括我,但是本人已经学到一种办法可以解决这个问题,来分享下,希望对你有多多少少的帮助! 如何在Java的 ...
- ios开发学习--歌词处理--解析lrc文件
我觉得要想解析lrc 首先大家应该了解一下lrc文件的结构,大家可以去看一下**百科 我这里粗略的写一下: ■ 时间标签(Time-tag) 形式为"[mm:ss]"(分钟数:秒数 ...
- Qt Style Sheets Examples(官方例子目录,很全)
Contents Style Sheet Usage Customizing the Foreground and Background Colors Customizing Using Dynami ...
- ASP.NET Routing
ASP.NET Routing Other Versions ASP.NET routing enables you to use URLs that do not have to map to ...
- BZOJ 1071 [SCOI2007]组队
1071: [SCOI2007]组队 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1330 Solved: 417[Submit][Status][ ...