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. Java学习笔记-Thread-线程

    //2015年5月5日16:55:00 //Main package com.alfredsun.thread; public class Main { public static void main ...

  2. Linux学习笔记-epoll

    #include <sys/epoll.h> epoll是Linux内核的一个系统调用,一种可扩展的I/O事件通知机制,最早在Linux内核2.5.44版本引入. 它的功能是监视多文件描述 ...

  3. [转]Numpy使用MKL库提升计算性能

    from:http://unifius.wordpress.com.cn/archives/5 系统:Gentoo Linux (64bit, Kernel 3.7.1)配置:Intel(R) Cor ...

  4. MATLAB初体验

    好激动 要入MATLAB的大坑了 恩 很遗憾第一个程序并不是hello world 好 插入代码里并没有MATLAB这个选项 这是一种歧视 x=[:pi/:*pi]; y=sin(x); plot(x ...

  5. LoadLibraryW 参数问题

    error C2664: "LoadLibraryW": 不能将参数1 从"const char [8]"转换为"LPCWSTR" 右击工程 ...

  6. Let's Encrypt(开源SSL证书管理工具)

    刚装上一个StartSSL 的证书没几天,却看到官方发出这样的通知: 无聊中看到了网上各种相关的扯淡: http://tieba.baidu.com/p/4801786642 http://www.t ...

  7. drag

    1.draggable ="true"  元素可以拖拽了 2.拖拽元素事件: dragstart  拖拽前触发 drag 拖拽前.拖拽结束之间,连续触发 dragend 拖拽结束触 ...

  8. java-进程

    一个java进程,只有一个入口,就是main方法. tomcat是一个java进程,tomcat只有一个入口,org.apache.catalina.startup.Bootstrap  类的main ...

  9. 查询--游标 limit skip sort

    打印出所有的里程: var cursor = db.tblDaily.find(); cursor.forEach(function(x){ print(x.DailyCount + x.DailyU ...

  10. 疯狂java讲义--笔记

    第一章.Java语言概述与开发环境 什么是软件:一系列按照特定顺序组织的计算机数据和指令的集合: 交互方式:两种 GUI(Graphical User Interface) 图像界面 .CLI (Co ...