leetcode 024 —— Swap Nodes in Pairs
来源:互联网 发布:数据库设计入门经典 编辑:程序博客网 时间:2024/04/29 19:01
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.
思路:三指针 替换 即可
class Solution {public:ListNode* swapPairs(ListNode* head) {if (!head || !head->next) return head;ListNode *newhead = new ListNode(0);ListNode *pre = newhead;pre->next = head;ListNode *l1 = head;ListNode *l2 = head->next;while (l1&&l2){l1->next = l2->next;pre->next = l2;l2->next = l1;pre = l1;l1 = l1->next;if(l1) l2 = l1->next;}return newhead->next;delete newhead;}};
0 0
- LeetCode——Swap Nodes in Pairs
- LeetCode——Swap Nodes in Pairs
- LeetCode—Swap Nodes in Pairs
- leetcode 024 —— Swap Nodes in Pairs
- 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——24—— Swap Nodes in Pairs
- LeetCode题解——Swap Nodes in Pairs
- leetcode第24题——**Swap Nodes in Pairs
- LeetCode算法题——24. Swap Nodes in Pairs
- LeetCode刷题——Swap Nodes in Pairs(Medium)
- 平衡二叉树
- python 读取文件
- JAVA实现实时Process,实时获取输出
- android Fragment
- cocos2d-x,CCArray源码学习
- leetcode 024 —— Swap Nodes in Pairs
- ListView item 不响应的原因
- ORA-27125: unable to create shared memory segment
- oracle连接excel的问题
- java 类型的并发程序 内存设置
- Android常用高级控件之——Fragment
- mybatis模糊查询、区间查询
- Swift学习笔记系列——(6)闭包
- sql学习笔记:navicat for mySql是使用