leetcode 24. Swap Nodes in Pairs
2016-03-08 14:40
621 查看
题意
交换链表相连两个点的值题解
如题代码
/** * 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) return NULL; ListNode *now = head->next, *pre = head; while(now) { swap(now->val, pre->val); if(now->next == NULL || (now->next != NULL && now->next->next == NULL)) break; pre = now->next; now = pre->next; } return head; } };
相关文章推荐
- [nodejs] cookie操作
- Wrtnode 配置方法:
- [LeetCode][JavaScript]Swap Nodes in Pairs
- [leetcode] 237. Delete Node in a Linked List
- Linode主机搭建(一)—— 注册账号和申请机器
- NodeJs初识模块开发
- 关于node引入到项目中的交流对话
- nodejs学习笔记四——express-session
- nodejs学习笔记三——nodejs使用富文本插件ueditor
- nodejs学习笔记一——nodejs安装
- 三种Node.js写文件的方式
- node下mysql入门
- LeetCode : Swap Nodes in Pairs [java]
- 三种Node.js写文件的方式
- Node.js笔记之模块机制
- [C语言][LeetCode][19]Remove Nth Node From End of List
- leetcode:Populating Next Right Pointers in Each Node II 【Java】
- 237. Delete Node in a Linked List
- leetcode:Populating Next Right Pointers in Each Node 【Java】
- 237. Delete Node in a Linked List