Swap Nodes in Pairs
来源:互联网 发布:杭州大名软件怎么样 编辑:程序博客网 时间:2024/05/21 07:00
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.
Solution:
/** * 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) { if(!head || !head->next) return head; ListNode* lead = new ListNode(-1); lead->next = head; ListNode *p = lead, *tmp; while(head && head->next) { tmp = head->next; head->next = head->next->next; tmp->next = head; p->next = tmp; head = head->next; p = p->next->next; } return lead->next; }};
0 0
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes In Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Apple开发者文档中关于UIApplication/UIScreen/UIDevice的简述
- 第一篇文章
- 【整理】Python中:self和__init__的含义 + 为何要有self和__init__
- python 导入自定义包 包机制
- 黑马程序员—-C语言入门十重奏之六相濡以沫
- Swap Nodes in Pairs
- JQuery
- linux重新编译安装gd,增加freetype支持,解决验证码不显示问题,Fatal error: Call to undefined function imagettftext()
- Cookie/Session机制详解
- Hdu 1162 Eddy's picture【最小生成树】
- UVA 10369Arctic Network【最小生成树】
- leetcode#195-tenthline
- 多维数组的定义和存储
- QList类介绍