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 ...
随机推荐
- Css学习(4)
文档流(标准流) 元素自上而下,自左而右,块元素独占一行,行内元素在一行上显示,碰到父集元素的边框换行. 浮动布局 float: left | right 特点: ★元素浮动之后不占据原来的 ...
- 手动向Maven本地仓库添加ORACLE jdbc驱动
一,当在maven工程中我们需要连接数据库的时候,只需要配置它的依赖就行,但是Maven不提供Oracle JDBC driver,由于Oracle授权问题它并没有提供,需要自己手动添加. 二,获取连 ...
- Win10开发环境配置
基本环境变量配置:最近工作电脑固态硬盘损坏了,导致不得不重新装机,从前配置的环境需要重新配置,在此标记. Java环境配置: 添加变量 JAVA_HOME=D:\Program Files\Java\ ...
- EventBus的使用;消息传递之EventBus;
EventBus传递消息(数据)和广播有点像,对广播传递数据有兴趣的可以看一下:Android数据传递,使用广播BroadcastReceiver: 1.添加build.gradle implemen ...
- Struts2学习:值栈(value stack)
1.index.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %& ...
- vue 整合element-ui
本文主要介绍如何在vue框架中结合elementUI. 本文主要参考: http://element-cn.eleme.io/#/zh-CN/component/quickstart 1.阅读本文 ...
- .NET MVC 保存Session值,6位数验证码
//6位数验证码: Random rm = new Random(); , ).ToString(); //MVC控制器Action中 保存session值 System.Web.HttpContex ...
- SPOJ3276 D-query
题意:n个数 a1...an,q组询问,每组询问给定 l,r,输出 [ l, r ] 有多少不同的数 ( n ≤30000, q ≤200000, ai ≤ 106 ) 离线 + 树状数组维护 #in ...
- 【Selenium-WebDriver自学】Selenium-IDE模式匹配(六)
==================================================================================================== ...
- C#取整函数Math.Round、Math.Ceiling和Math.Floor
1.Math.Round:四舍六入五取偶 引用内容 Math.Round(0.0) //0Math.Round(0.1) //0Math.Round(0.2) //0Math.Round(0.3) / ...