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. 常见IT面试题

    1.爬楼梯问题,有个N阶的楼梯,一个人可以一次爬1阶,也可以爬2阶,求问总计有多少种爬法? F(N)= F(N-1)+F(N-2). N=1时,有1种爬法,N=2时,有2种爬法.该题可以用递归方法求解 ...

  2. [Q]自定义快捷键

    打开CAD批量打图精灵主界面可以使用以下三个命令其一:“QuickPlot”.“QPlot”.“QP”.“PP”,其中“PP”可以更改, 方法如下:进入AutoCAD传统界面,点“工具”-“自定义”- ...

  3. 如何安装chrome扩展?比如json-handle插件如何安装

    chrome插件安装方法: 方式一,在线安装 直接插到json-handle地址,添加即可 https://chrome.google.com/webstore/detail/json-handle/ ...

  4. DOM小解

    现在来说说DOM 文档对象模型DOM(Document Object Model)定义访问和处理html文档的标准方法.DOM将html文档呈现为带有元素   ,属性和文本的树结构(节点树) 先来看看 ...

  5. css基础和心得(二)

    css中的某些样式是具有继承性的.它允许样式不仅应用于某个特定html标签元素 而且应用于其后代.如: p{color:red;}  <p>dsffd<spans>sdfasd ...

  6. 专注VR/AR广告 ,内容感知广告公司Uru获80万美元投资

    随着AR/VR技术不断地跃进,越来越多的公司开始运用这项技术为消费者提供广告和营销信息.Uru是一家打造计算机视觉驱动内容广告的公司,专注于数字视频和VR/AR类似的沉浸式媒介,就在刚刚这家公司宣布完 ...

  7. 【POJ2186】受牛仰慕的牛

    受牛仰慕的牛(popular cows)  每头牛都有一个梦想:成为一个群体中最受欢迎的名牛!在一个有N(1<=N<=10,000)头牛的牛群中,给你M(1<=M<=50,00 ...

  8. Playmaker 基础使用与案例操作

    首先是把下载好的插件导入Unity工程中. ▼导入完成后第一个动作就是检查下拉菜单里面是否已经增加了Playmaker的功能,如果在安装后没看到Playmaker的菜单,一般情况下直接点击菜单上的空白 ...

  9. 【bootstrap】时间选择器datetimepicker和daterangepicker

    在bootstrap中的时间选择器有两种:dateTimePicker和dateRangePicker 1.dateTimePicker好像是官方嫡插件:   需要的文件: <link rel= ...

  10. Windows 多用户远程访问 Ubuntu 14.04桌面

    使用X2Go实现多用户远程访问 Ubuntu 14.04桌面:VNC也可以,但是每次连接VNC就回新创建一个Seession,想要在下次远程登录的时候返回上次活动,需要记住开启的线程,这种繁琐的操作不 ...