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.

题目大意:给定一个单链表,交换每两个元素。

解题思路:采用头插法重新构建这个链表,每插入两个就把头指针移动到最后,再插入新的节点。

Talk is cheap>>

    public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode res = new ListNode(0);
res = head.next;
ListNode cur = new ListNode(0);
ListNode tmp = head;
ListNode st = head;
int cnt = 1;
while (cur != null && st != null) {
tmp = st.next;
st.next = cur.next;
cur.next = st;
st = tmp;
if (cnt == 2) {
cnt = 0;
cur = cur.next.next;
}
cnt++;
}
return res;
}

Swap Nodes in Pairs——LeetCode的更多相关文章

  1. Swap Nodes in Pairs leetcode

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  2. Swap Nodes in Pairs leetcode java

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

  3. Swap Nodes in Pairs LeetCode题解

    做完这个题目,感觉LeetCode的题目出的真好... 这种题,如果让我在面试时候纸上写代码,肯定会挂的. 我昨天晚上看的题目,昨天脑子是懵的,放下了.今天早上来做. 一开始做,提交,果然错了.写的代 ...

  4. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

  5. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

  6. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  7. 【LeetCode练习题】Swap Nodes in Pairs

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  8. leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法

    Swap Nodes in Pairs  Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  9. Leetcode 线性表 Swap Nodes in Pairs

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...

随机推荐

  1. 每次打开VS2010都会报“ devenv.exe -Assert Failure”这个错误

    把.net framework4.5中文包卸载掉,, 如果还不行就把.net framework4.5也卸载掉,,然后到微软官网下载net framework4.5安装包安装,安装完后把中文包卸载掉就 ...

  2. HDFS的Java客户端操作代码(查看HDFS下所有的文件存储位置信息)

    1.查看HDFS下所有的文件存储位置信息 package Hdfs; import java.net.URI; import org.apache.hadoop.conf.Configuration; ...

  3. Conversion Between DataTable and List in C#

    1.List to DataTable public static DataTable ToDataTable<TSource>(this IList<TSource> dat ...

  4. Activity间的跳转,startActivity与startActivityForResult

    JreduCh04 2016-07-30跳转 (由一个画面跳转到另一个画面)两种方法:Intent中 startActivity.startActivityForResult.后者可设置request ...

  5. C#获取类中所有方法

    var t = typeof(HomeController); //获取所有方法 System.Reflection.MethodInfo[] methods = t.GetMethods(); // ...

  6. ssh框架配置事务管理器

    http://blog.163.com/zsq303288862@126/blog/static/9374596120111182446727/

  7. Linux下inotify监控文件夹状态,发生变化后触发rsync同步

    1.安装工具--inotifywget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar ...

  8. 推荐Asp.net WebApi入门教程

    Web API 强势入门指南; Web API 入门指南 - 闲话安全; 实例快速上手 -ASP.NET 4.5新特性WebAPI从入门到精通; Asp.net WebApi 项目示例(增删改查).

  9. iPhone中如何判断当前相机是否可用

    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; if (![UIImag ...

  10. WordPress4.1新的函数介绍

    wordpress 4.1也更新了一阵子了,作为一般用户最关系的就是新的wordpress主题,作为开发者,新增的函数也给我们带来了更多的便捷和惊喜,今天就转载一篇介绍wordpress4.1中新增的 ...