Swap Nodes in Pairs

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.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

 

C++实现代码:

#include<iostream>
#include<new>
using namespace std;

//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==NULL||head->next==NULL)
            return head;
        ListNode *p=head;
        ListNode *pre=head;
        ListNode *qq=NULL;
        ListNode *q=NULL;
        while(p&&p->next)
        {
            q=p->next;
            p->next=NULL;
            qq=q->next;
            q->next=NULL;
            q->next=p;
            p->next=qq;
            if(p==head)
            {
                head=q;
            }
            else
                pre->next=q;
            pre=p;
            p=p->next;
        }
        return head;
    }
    void createList(ListNode *&head)
    {
        ListNode *p=NULL;
        int i=0;
        int arr[10]= {7,5,6,4,1,3,9,8,2,10};
        for(i=0; i<2; i++)
        {
            if(head==NULL)
            {
                head=new ListNode(arr[i]);
                if(head==NULL)
                    return;
            }
            else
            {
                p=new ListNode(arr[i]);
                p->next=head;
                head=p;
            }
        }
    }
};

int main()
{
    Solution s;
    ListNode *L=NULL;
    s.createList(L);
    ListNode *head=L;
    while(head)
    {
        cout<<head->val<<" ";
        head=head->next;
    }
    cout<<endl;
    L=s.swapPairs(L);
    while(L)
    {
        cout<<L->val<<" ";
        L=L->next;
    }
}

运行结果:

时间: 2024-08-01 07:32:52

Swap Nodes in Pairs的相关文章

[LeetCode]24.Swap Nodes in Pairs

[题目] 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. Your algorithm should use only constant space. You may not modify the values in the lis

LeetCode 24 Swap Nodes in Pairs(交换序列中的结点)

翻译 给定一个链表,调换每两个相邻节点,并返回其头部. 例如, 给定 1->2->3->4, 你应该返回的链表是 2->1->4->3. 你的算法必须使用唯一不变的空间. 你也不能修改列表中的值,只有节点本身是可以改变的. 原文 Give a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should

&lt;font color=&quot;red&quot;&gt;[置顶]&lt;/font&gt;

Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸,本博客会持续更新,感谢您的支持,欢迎您的关注与留言.博客有多个专栏,分别是关于 Windows App开发 . UWP(通用Windows平台)开发 . SICP习题解 和 Scheme语言学习 . 算法解析 与 LeetCode等题解 . Android应用开发 ,而最近会添加的文章将主要是算法和Android,不过其它内容也会继续完善. About the Author 独立 Windows App 和

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

The nodes should be added to the underlying vendor clusterware before adding them to the CRS cluster

[oracle@m1 ~]$ /u01/oracle/11.2.3/db/oui/bin/addNode.sh "CLUSTER_NEW_NODES={m2}" Starting Oracle Universal Installer... Checking swap space: must be greater than 500 MB.   Actual 8811 MB    Passed Oracle Universal Installer, Version 11.2.0.3.0 P

Oracle诊断案例-SGA与Swap之二

oracle     link: http://www.eygle.com/case/sga2.htm 案例描述: 这是一个大型生产系统问题出现时系统累计大量用户进程用户请求得不到及时响应,新的进程不断尝试建立连接连接数很快被用完 数据库版本:9.2.0.3操作系统:Solaris8   1.检查alert文件 日志中记录如下错误信息,说明磁盘异步IO出现问题:   WARNING: aiowait timed out 2 timesTue Aug 26 15:33:32 2003WARNING

Oracle诊断案例-SGA与Swap之一

oracle     link: http://www.eygle.com/case/sga1.htm 案例描述: 用户报告,服务器启动一段时间以后,无法建立数据库连接重新启动几分钟以后,再次无法连接 系统无法正常使用. 1.登陆系统 SunOS 5.8 login: rootPassword: Last login: Tue Mar 23 13:56:59 from 172.16.31.41Sun Microsystems Inc. SunOS 5.8 Generic Patch Octobe