Swap Nodes in Pairs

原创 2015年07月09日 10:02:11

写法一:

/**
 * 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* h1 = head, *parent = NULL, *h3 = NULL;
        while(h1!=NULL && h1->next!=NULL)
        {
            h3 = h1->next->next;
            
            //swap h1 & h1->next
            if(parent == NULL)
                head = h1->next;
            else
                parent->next = h1->next;
            
            h1->next->next = h1;
            h1->next = h3;

            parent = h1;
            h1 = h3;
        }
        return head;
    }
};

写法二:

/**
 * 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(NULL == head || NULL == head->next)
            return head;
        ListNode* h1 = head;
        head = head->next;
        ListNode* h3 = head->next;
        head->next = h1;
        h1->next = h3;
         
        ListNode* parent = h1;
        h1 = h3;
       
        while(h1!=NULL)
        {
            if(h1->next == NULL)
                break;
            h3 = h1->next->next;
            
            //swap h1 & h2
            parent->next = h1->next;
            h1->next->next = h1;
            h1->next = h3;
            
            
            parent = h1;
            h1 = h3;
        }
        return head;
    }
};

效率:


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Swap Nodes in Pairs (LeetCode Algorithm Problems)

这道题应该是一道编程语言题,因为要解决的问题是链表的操作。原题链接Given a linked list, swap every two adjacent nodes and return its h...

LeetCode | Swap Nodes in Pairs(将链表中的元素两两交换)

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-...

Swap Nodes in Pairs

题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, ...

Swap Nodes in Pairs--LeetCode

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->...

LeetCode24. Swap Nodes in Pairs

LeetCode24. Swap Nodes in Pairs

Leetcode-Swap Nodes in Pairs

作者:disappearedgod 文章出处:http://blog.csdn.net/disappearedgod/article/details/23793109 时间:2014-4-...

LeetCode - Swap Nodes in Pairs

题注 这道题吧,看起来挺简单的,但是里面涉及到一些细节问题,很容易就错了,在实现之前最好仔细考虑一下各个细节。 问题 Given a linked list, swap every two adja...

Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-...

【LeetCode】Swap Nodes in Pairs

题目描述:Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2...

( Leetcode 24) Swap Nodes in Pairs

题目:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)