24. Swap Nodes in Pairs
来源:互联网 发布:大数据相关技术 编辑:程序博客网 时间:2024/06/05 20:08
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.
/** * 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 == NULL || head->next == NULL) return head; ListNode dummy(-1); dummy.next = head; for (ListNode *prev = &dummy, *cur = prev->next, *next = cur->next; next; prev = cur, cur = cur->next, next = cur ? cur->next : NULL) { prev->next = next; cur->next = next->next; next->next = cur; } return dummy.next; }};
0 0
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24.Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- Sqoop教程(一) Sqoop数据迁移工具
- python 通过post方式上传文件到php服务器
- 数据挖掘中的时序数据分析方法(According to Eamonn Keogh’s Time Series Tutorial)
- 1078. Hashing (25)
- sevureCRT乱码
- 24. Swap Nodes in Pairs
- JavaScript判断对象数组是否包含对象
- Fragment的用法
- GIT远程仓库基本操作(1)
- 命令模式c++实现
- 深入浅出Mybatis-改造Cache
- 异常Throwable(Exception/Error)
- JVM内存管理之内存结构
- Install CM13 ROM on Huawei Honor 5X