[LeetCode]024-Swap Nodes in Pairs
来源:互联网 发布:win7怎样建立网络共享 编辑:程序博客网 时间:2024/05/30 23:48
题目:
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:
ListNode* swapPairs(ListNode* head) { if(head == NULL || head->next == NULL) return head; ListNode * temp = head; while(temp != NULL) { if(temp->next !=NULL)//数目是奇数时,比如1 2 3,到3位置后面是空就break { swap(temp->val,temp->next->val); temp = temp->next->next; } else break; } return head; }
0 0
- LeetCode 024 Swap Nodes in Pairs
- 【LeetCode OJ 024】Swap Nodes in Pairs
- leetcode 024 swap nodes in pairs
- 024 Swap Nodes in Pairs [Leetcode]
- LeetCode 024 Swap Nodes in Pairs
- [LeetCode]024-Swap Nodes in Pairs
- LeetCode 024 Swap Nodes in Pairs
- LeetCode 024 Swap Nodes in Pairs
- LeetCode-024 Swap Nodes in Pairs
- LeetCode:Swap Nodes in Pairs
- LeetCode: Swap Nodes in Pairs
- [Leetcode] Swap Nodes in Pairs
- LeetCode : Swap Nodes in Pairs
- [LeetCode] Swap Nodes in Pairs
- Leetcode: Swap nodes in pairs
- [LeetCode]Swap Nodes in Pairs
- [LeetCode]Swap Nodes in Pairs
- LeetCode-Swap Nodes in Pairs
- 扫雷2
- CSU1256 天朝的单行道(spfa)
- 蓝懿iOS 技术交流和心得分享 12.8
- 北大OJ1003
- Linux学习笔记(十七、Shell概述)
- [LeetCode]024-Swap Nodes in Pairs
- Gradle DSL method not found: 'android()'
- nyoj--19--擅长排列的小明(dfs)
- [TwistedFate]数据库初级使用
- 北大OJ1004
- MIT6.828 LAB4 PartB
- 测试letsencrypt的https功能ssl
- Lua与VC的交互
- eclipse和MyEclipse的插件安装