1. Swap Nodes in Pairs My Submissions QuestionEditorial Solution

    Total Accepted: 95230 Total Submissions: 270562 Difficulty: Easy

    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.

Subscribe to see which companies asked this question

思路:简单,学会指针间的操作和指向

/**
* 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 *beg = head->next,*p=head,*pre=head;
while(p!=NULL&&p->next!=NULL)
{
ListNode *p_next_next = p->next->next;
pre->next = p->next;
p->next->next = p;
p->next = p_next_next;
pre = p;
p = p->next;
}
return beg;
}
};

34. Swap Nodes in Pairs的更多相关文章

  1. Leetcode-24 Swap Nodes in Pairs

    #24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  2. 24. Swap Nodes in Pairs

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  3. [LintCode] Swap Nodes in Pairs 成对交换节点

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

  4. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  5. 【LeetCode练习题】Swap Nodes in Pairs

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  6. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

  7. leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法

    Swap Nodes in Pairs  Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  8. Leetcode 线性表 Swap Nodes in Pairs

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...

  9. leetcode-algorithms-24 Swap Nodes in Pairs

    leetcode-algorithms-24 Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and re ...

随机推荐

  1. [no_code][Alpha]发布声明报告

    项目 内容 2020春季计算机学院软件工程(罗杰 任健) 2020春季计算机学院软件工程(罗杰 任健) 作业要求 发布声明 我们在这个课程的目标是 设计出一个OCR表单处理软件 这个作业在哪个具体方面 ...

  2. the Agiles Scrum Meeting 9

    会议时间:2020.4.17 20:00 1.每个人的工作 今天已完成的工作 个人结对项目增量开发组:基本实现个人项目创建.仓库自动分配,修复bug issues: 增量组:准备评测机制,增加仓库自动 ...

  3. Noip模拟62 2021.9.26

    T1 Set 真就随机化拿了$90$?? 不过还是有依据的,毕竟这道题出解的几率很大,随出答案的概率也极大 所以不妨打一个随机化 1 #include<bits/stdc++.h> 2 # ...

  4. 单片机零基础学习之从“点灯”入门STM32

    本篇文章通过一个简单的例子来熟悉模块化编程以及利用库函数的方法进行开发使用STM32外设的基本流程. 首先,我们打开本讲的例程,在工程目录我们可以看到驱动分组下有 led.delay 两个.c源文件, ...

  5. 基础篇:JAVA集合,面试专用

    没啥好说的,在座的各位都是靓仔 List 数组 Vector 向量 Stack 栈 Map 映射字典 Set 集合 Queue 队列 Deque 双向队列 关注公众号,一起交流,微信搜一搜: 潜行前行 ...

  6. 最大连续数列和 牛客网 程序员面试金典 C++ Python

    最大连续数列和 牛客网 程序员面试金典 C++ Python 题目描述 对于一个有正有负的整数数组,请找出总和最大的连续数列. 给定一个int数组A和数组大小n,请返回最大的连续数列的和.保证n的大小 ...

  7. Linux 内核网桥源码分析

    Linux网桥源码的实现 转自: Linux二层网络协议 Linux网桥源码的实现 1.调用 在src/net/core/dev.c的软中断函数static void net_rx_action(st ...

  8. hdu 2154 跳舞毯(简单DP)

    题意: 有一个圆圆的毯,被平均分成三个扇形.分为标记为A,B,C. 小余从A开始跳,每次可跳到相邻的扇形上.(A->B 或 A->C) 问小余跳n次,最后回到扇形A的方案数是多少. 思路: ...

  9. WPF_02_XAML

    XAML(Extensible Application Markup Language的简写)是用于实例化.NET对象的标记语言.XAML对于WPF不是必须的. XAML基础 XAML标准: XAML ...

  10. shell 脚本静默安装oracle11g

    以下脚本的手动安装连接:  https://www.cnblogs.com/leihongnu/p/12698593.html [ #/bin/bash#安装日志touch /root/message ...