Leetcode【23】:Swap Nodes in Pairs
来源:互联网 发布:it培训计划方案 编辑:程序博客网 时间:2024/05/19 02:24
题目:给定一个链表,从头翻转2个相邻结点。比如1->2->3->4->5->6->7,返回2->1->4->3->6->5->7。
分析:简单的链表题目,搞定指针就行。
class Solution{public:ListNode *swapPairs(ListNode *head){if(head==NULL || head->next==NULL)return head;//特殊情况ListNode front(-1);front.next=head;//虚拟个头结点ListNode *pPrev=&front;//由于要改变1,2个结点的指针,因此要保存结点1的前nodeListNode *pNode=head;ListNode *pNext=head->next;while(pNext!=NULL){pNode->next=pNext->next;pPrev->next=pNext;pNext->next=pNode;//修改3个指针以实现翻转pPrev=pNode;pNode=pNode->next;pNext=pNode==NULL?NULL:pNode->next;//更新3个指针,要注意pNext的临界条件。}return front.next;}};
0 0
- LeetCode 23 Swap Nodes in Pairs
- Leetcode【23】: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
- 【LeetCode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- 复杂指针的声明
- 堆排序
- HTTP请求头详解
- ym——对象序列化(新)
- fzu Problem 2129 子序列个数
- Leetcode【23】:Swap Nodes in Pairs
- 数据挖掘
- hdu 3743 归并排序
- vi文本编辑器
- ASCII,Unicode和UTF-8
- mongoDB2.6,java使用详解
- hdu 1328
- ubuntu下PIL无法显示图片
- 骆驼命名法,帕斯卡命名法和匈牙利命名法