leetcode - Swap Nodes in Pairs

来源:互联网 发布:大数据治理 pdf 编辑:程序博客网 时间:2024/06/03 20:28

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.

分析:指针操作

class Solution {public:    ListNode *swapPairs(ListNode *head) {        ListNode *CurNode = head;        while(CurNode != NULL)        {            if(!CurNode->next)break;            int temp = CurNode->next->val;            CurNode->next->val = CurNode->val;            CurNode->val = temp;            CurNode = CurNode->next->next;        }        return head;    }};


0 0