leetcode系列(65)Swap Nodes in Pairs
来源:互联网 发布:长城软件怎么样 编辑:程序博客网 时间:2024/05/22 12:03
Given a linked list, swap every twoadjacent 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 constantspace. You may not modify the values in the list, only nodesitself can be changed.
解答:这个题目唯一增加的难度就是不准交互value,必须操作节点,用dummy_node的方式在操作链表的时候非常用,且会减少代码的难度。
class Solution {public: ListNode* swapPairs(ListNode* head) { ListNode dummy(0); dummy.next = head; ListNode* pre = &dummy; ListNode* cur = head; while (cur != nullptr && cur->next != nullptr) { ListNode* nxt = cur->next; cur->next = nxt->next; nxt->next = cur; pre->next = nxt; // update cur and pre pre = cur; cur = cur->next; } return dummy.next; }};
0 0
- leetcode系列(65)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
- 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
- LeetCode | Swap Nodes in Pairs
- 【leetcode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- LeetCode - Swap Nodes in Pairs
- 服务器+客户端软件+java邮件发送jar
- bzoj2749 外星人 数论
- Codevs_P1261 龙王的礼物(DP+背包)
- LeetCode 168:Excel Sheet Column Title
- Swift 下开启warning提示方法
- leetcode系列(65)Swap Nodes in Pairs
- PAT乙级 反转链表 (25)
- Android学习笔记-SharedPreferences
- PHP 正则表达式常用的函数
- 线检测
- adb shell 打开应用程序
- 崩溃问题:iOS9 This application is modifying the autolayout engine from a background thread, which
- Keil MDK之RTX系统时钟节拍和时间管理
- ccf+模版生成系统+字符串的大量应用