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->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;
* struct ListNode *next;
* };
*/
struct ListNode* swapPairs(struct ListNode* head) {
struct ListNode *tmp, *new_head = head, *tail = NULL;
while(NULL != head&&NULL != head->next){
tmp = head->next;
head->next = head->next->next;
tmp->next = head;
if(head == new_head){
new_head = tmp;
}
else{
tail->next = tmp;
}
tail = head;
head = head->next;
}
if(NULL != head&&NULL != tail){
tail->next = head;
}
return new_head;
}
LeetCode OJ 24. Swap Nodes in Pairs的更多相关文章
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 【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 ...
- 【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- 【LeetCode】24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【LeetCode OJ】Swap Nodes in Pairs
题目:Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2 ...
- LeetCode OJ:Swap Nodes in Pairs(成对交换节点)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- LeetCode:24. Swap Nodes in Pairs(Medium)
1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 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 ...
- 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 ...
随机推荐
- UI相关教程:HUD、UMG和Widget
转自:http://aigo.iteye.com/blog/2258612 蓝图脚本来处理 ================================================== 用UM ...
- Delphi StringGrid控件的用法
Delphi StringGrid控件 组件名称:StringGrid ●固定行及固定列: StringGrid.FixedCols:=固定行之数; StringGrid.Fixe ...
- c#数组用法
随机数: string[] str = new string[4]{"a","b","c","d"} Readom r ...
- Hbase 分布式环境安装部署
Hbase分布式集群搭建--安装步骤 这一步如果没有deploy.sh脚本的可以使用scp命令分别分发到其他节点去 到集群里看看安装好的hbase 使用脚本启动所有节点的zookeeper 启动HDF ...
- 《马哥出品高薪linux运维教程》wingkeung学习笔记-linux基础入门课程
计算机原理概念: 1.CPU和内存中的存储单元通信线路称为总线(BUS),总线是被指令和数据复用的,所以也称为前端总线. 2.计算机中计算频率的时间标准即晶体振荡器原理,精确计算时间长度,根据相同的时 ...
- Echarts动态加载柱状图和折线图混合展示的实例
一.引入echarts文件: <script type="text/javascript" src="echarts.js"></script ...
- while 循环,格式化输出,运算符(not,and,or)
一,while 循环 1. 循环. while循环 while 条件: 代码块(循环体) 执行流程: 1. 判断条件是否为真. 如果真. 执行代码块 2. 再次判断条件是否为真...... 3. 当条 ...
- python-模块的导入import
#-*- coding:utf-8 -*- #本次学习:模块的导入 ''' 1.模块名不能与第三方库或者本地库名字重名/冲突 2.导入模块时,寻找顺序:现在当前目录找,再去我们环境变量配置的pytho ...
- android 开发 实现一个app的引导查看页面(使用ViewPager组件)
我们安装完app后第一次打开app,通常都会有一个翻页图片形式的app引导简介说明.下面我们来实现这个功能.ViewPager这个组件与ListView和RecyclerView在使用上有很高的相似处 ...
- redis下操作hash对象
redis下操作hash对象 hash用于存储对象,对象的格式为键值对 命令 设置 设置单个属性 HSET key field value 设置多个属性 HMSET key field value [ ...