leetcode---swap-nodes-in-pairs---链表
来源:互联网 发布:农业大数据价值 编辑:程序博客网 时间:2024/05/17 06:08
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given1->2->3->4, you should return the list as2->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.
/** * 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) return NULL; ListNode *p = head; ListNode *pre = head; bool first = true; while(p && p->next) { ListNode *next = p->next; // next = 2 ; next = 4 ListNode *next2 = next->next; // next2 = 3 ; next2 = 5 next->next = p; // 2->1 ; 4->3 p->next = next2; // 1->4 ; 3->NULL if(first) { head = next; first = false; } else pre->next = next; // ; 1->4 pre = p; // pre = 1 ; pre = 3 p = next2; // p = 3 ; p = NULL } return head; }};
阅读全文
0 0
- [Leetcode 24 链表]Swap Nodes in Pairs
- leetcode Swap Nodes in Pairs 链表
- leetcode #24 swap nodes in pairs 链表
- [LeetCode][链表]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
- mysql的processlist中出现大量的query end状态
- 高德地图开发bug
- LOJ6005 网络流24题
- session一致性-将session存储到redis中
- hdu 5927 Auxiliary Set dfs+逆向思维
- leetcode---swap-nodes-in-pairs---链表
- oc预处理和编译的理解
- CodeVs 2245 股票趋势 (二维线段树 zkw)
- 贪心(11.22)
- JDK8安装
- PHP设计模式---单例模式
- 我说编程细节
- 两位数加法
- LOJ6226 网络流24题 骑士