Leetcode 24. Swap Nodes in Pairs (Easy) (cpp)
2016-07-12 18:06
393 查看
Leetcode 24. Swap Nodes in Pairs (Easy) (cpp)
Tag: Linked List
Difficulty: Easy
Tag: Linked List
Difficulty: Easy
/* 24. Swap Nodes in Pairs (Easy) 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 */ /** * 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* temp = head -> next; head -> next = swapPairs(temp -> next); temp -> next = head; return temp; } };
相关文章推荐
- node.js windows下npm配置全局模块express.
- 对express中next函数的一些理解
- Build a RESTful API Using Node and Express 4
- Node.js-中文分词【1】-node-segment
- node.js安装
- Leetcode 237. Delete Node in a Linked List (Easy) (cpp)
- node -- all
- nodejs 常用 index
- 通过sed和awk获取集群最新挂掉的DataNode信息
- nodejs:express API之res.locals
- C#操作XML文档(XmlDocument、XmlNode、XmlAttribute、SelectSingleNode、SelectNodes、XmlNodeList)
- 116. Populating Next Right Pointers in Each Node && 117. Populating Next Right Pointers in Each Node II
- Leetcode 19. Remove Nth Node From End of List (Easy) (cpp)
- nodejs下的图形处理库gm在windows环境的安装
- NODEJS mongoose 增删改查
- hadoop节点nodemanager启动失败原因小结~
- 关于nodejs的npm命令无反应的解决方案
- LeetCode - 24. Swap Nodes in Pairs
- 基于nodeJs express 框架的图片上传和编辑(引用美图编辑工具)上传小示例
- 理解inode