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.

题意:倒转链表中相邻的两个值

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* swapPairs(struct ListNode* head) {
int flag=,tmp=;
struct ListNode* p=head;
while(p!=NULL){
if(==flag&&p->next!=NULL){
tmp=p->val;
p->val=p->next->val;
flag=;
}
else if(==flag){
p->val=tmp;
flag=;
}
p=p->next;
}
return head;
}

【LeetCode】24. Swap Nodes in Pairs的更多相关文章

  1. 【LeetCode】24. Swap Nodes in Pairs (3 solutions)

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

  2. 【一天一道LeetCode】#24. Swap Nodes in Pairs

    一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  3. 【LeetCode】024. Swap Nodes in Pairs

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

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

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

  5. 【leetcode❤python】24. Swap Nodes in Pairs

    #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):#     def __init ...

  6. LeetCode OJ 24. Swap Nodes in Pairs

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

  7. LeetCode:24. Swap Nodes in Pairs(Medium)

    1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...

  8. 24. Swap Nodes in Pairs

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

  9. 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)

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

随机推荐

  1. 【转】关于JVM CPU资源占用过高的问题排查

    http://my.oschina.net/shipley/blog/520062 一.背景: 先执行一个java程序里面开了两个线程分别都在while循环做打印操作. ? 1 # java -cp  ...

  2. 【01背包】HDU 2546 饭卡

    Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) ...

  3. C#笔记(二)变量

    C#把数据类型分为两种 ● 值类型 ● 引用类型 从概念上看:其区别是值类型直接存储其值,而引用类型存储对值的引用 这两种类型存储在内存的不同地方:值类型存储在堆栈中,而引用类型存储在托管堆上 如果变 ...

  4. MyEclipse 中文注释乱码

    window-->preference-->general-->content type然后在<Content Types>中展开每一个子项,并在<Default ...

  5. java静态方法之线程安全问题

    静态方法和实例方法的区别是静态方法只能引用静态变量,静态方法通过类名来调用,实例方法通过对象实例来调用 每个线程都有自己的线程栈,栈与线程同时创建,每一个虚拟机线程都有自己的程序计数器PC,在任何时刻 ...

  6. bootstrap的总结1 - 网格系统

    1.Bootstrap 网格系统 1)下表总结了 Bootstrap 网格系统如何跨多个设备工作: 2)Bootstrap 网格的基本结构 <div class="container& ...

  7. Base64encode

    一直以来Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.BASE64Encoder/BASE64Decoder类.这个类是 ...

  8. Tiny6410之LED裸机驱动

    操作步骤: 第一步:查看开发板电路原理图 找到LED 的管脚所对应的寄存器 nLED_1 - GPK4 nLED_2 - GPK5 nLED_3 - GPK6 nLED_4 - GPK7 由原理图可知 ...

  9. 杭州蓝松科技推出的安卓端的USB转串口调试助手, 欢迎下载使用

    杭州蓝松科技推出的安卓端的USB转串口调试助手, 欢迎下载使用 下载地址:http://files.cnblogs.com/guobaPlayer/%E8%93%9D%E6%9D%BEUSB%E4%B ...

  10. rpm 安装包制作

    rpm 安装包制作 思路 参照系统自带 etcd 解压->替换掉执行文件->打包 1 预备安装工具 下载工具 yumloader #yum install -y yum-utils 解压工 ...