LeetCode OJ--Swap Nodes in Pairs
2014-08-16 20:57
363 查看
https://oj.leetcode.com/problems/swap-nodes-in-pairs/
链表的处理
链表的处理
/** * 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) { ListNode *dummy = new ListNode(-1); if(head == NULL) return dummy->next; dummy->next = head; ListNode *tail = dummy; ListNode *current = head; ListNode *second = head->next; while(current && second) { tail->next = second; current->next = second->next; second->next = current; current = current->next; if(current == NULL) break; second = current->next; tail = tail->next; tail = tail->next; } return dummy->next; } };
相关文章推荐
- LeetCode OJ - Swap Nodes in Pairs
- 【LeetCode OJ】Swap Nodes in Pairs
- [LeetCode OJ]Swap Nodes in Pairs
- 【LeetCode OJ】Swap Nodes in Pairs
- [LeetCode OJ]Swap Nodes in Pairs
- Leetcode OJ 24 Swap Nodes in Pairs [Medium]
- LeetCode OJ:Swap Nodes in Pairs
- LeetCode OJ:Swap Nodes in Pairs(成对交换节点)
- LeetCode OJ 之 Swap Nodes in Pairs (交换成对相邻结点的值)
- Swap Nodes in Pairs
- LeetCode-- Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- leetcode 24. Swap Nodes in Pairs
- [LeetCode] Swap Nodes in Pairs
- Swap Nodes in Pairs
- Leetcode-24. Swap Nodes in Pairs
- 重拾编程之路--24、Swap Nodes in Pairs
- 【LeetCode】24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- LeetCode Swap Nodes in Pairs