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.

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

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * struct ListNode *next;
  6. * };
  7. */
  8. struct ListNode* swapPairs(struct ListNode* head) {
  9. int flag=,tmp=;
  10. struct ListNode* p=head;
  11. while(p!=NULL){
  12. if(==flag&&p->next!=NULL){
  13. tmp=p->val;
  14. p->val=p->next->val;
  15. flag=;
  16. }
  17. else if(==flag){
  18. p->val=tmp;
  19. flag=;
  20. }
  21. p=p->next;
  22. }
  23. return head;
  24. }

【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. 使用Gradle编译release apk报错:Please correct the above warnings first

    在开发SDK的过程中,遇到了一个研发,使用了自己的SDK之后遇到了各种问题,于是我们自己帮忙接入. 所有代码都接入完成之后,准备export出一个release包,但是此时却报错: 此时出现了很多的w ...

  2. To the end

    身为一名初二狗的我也走过了半年.不管怎么说人生中也没有几个半年嘛.从九月到现在快四个月了,我也离中考越来越近了/郁闷/.但是还是要好好过唔.不过我想起这半学期还是挺充实的,至少没有浪费太多的时间.有些 ...

  3. MariaDB数据解压版安装(10.0.16)

    官网下载地址:https://downloads.mariadb.org/    (自己选择版本下载) 在windows 7 下安装 1.下载到解压版安装文件mariadb-10.0.16-win32 ...

  4. 微信小程序推荐

    工具类 快递100助手:查快递,寄快递. 驾考帮手:为您提供科目一.科目四的测试题练习. 翻译e:多国语言在线翻译,中英.中日.中俄.中法快速翻译,专业翻译. 朝夕日历Pro:一个日历管理 app,你 ...

  5. NGINX----源码阅读---config配置脚本

    config文件为nginx的配置入口文件. 1. #!/bin/sh # Copyright (C) Igor Sysoev # Copyright (C) Nginx, Inc. LC_ALL=C ...

  6. 《HTML5权威指南》

    <HTML5权威指南> HTML元素: html字符实体 html全局属性 html base标签 用元数据元素说明文档 标记文字(第八章) 标记文字.组织内容.文档分节 表格元素 表单元 ...

  7. Python学习笔记——进阶篇【第八周】———进程、线程、协程篇(Socket编程进阶&多线程、多进程)

    本节内容: 异常处理 Socket语法及相关 SocketServer实现多并发 进程.线程介绍 threading实例 线程锁.GIL.Event.信号量 生产者消费者模型 红绿灯.吃包子实例 mu ...

  8. MSSQL2008 中文乱码问题 (引自ljg888的专栏)

    PHP向MSSQL2008中写入数据,中文乱码   首先:查看SQLserver编码格式的SQL语句为:     SELECT  COLLATIONPROPERTY('Chinese_PRC_Stro ...

  9. html布局,左侧固定右侧自适应

    前几天看到我们的UI稿,要实现左侧固定树结构,右侧自适应.想着自己写过几次但是每次都会忘记,在这里做个笔记. 第一种方法: <!DOCTYPE html> <html lang=&q ...

  10. dll间接应用问题

    在项目prj引用一个dll,a.dll,时,此dll应用b.dll 此时不将b.dll引用添加到prj,会有问题