题目:

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.http://i.cnblogs.com/EditPosts.aspx?opt=1

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

题解:

这道题考察了基本的链表操作,注意当改变指针连接时,要用一个临时指针指向原来的next值,否则链表丢链,无法找到下一个值。

本题的解题方法是:

需要运用fakehead来指向原指针头,防止丢链,用两个指针,ptr1始终指向需要交换的pair的前面一个node,ptr2始终指向需要交换的pair的第一个node。

然后就是进行链表交换。

需要用一个临时指针nextstart, 指向下一个需要交换的pair的第一个node,保证下一次交换的正确进行。

然后就进行正常的链表交换,和指针挪动就好。

当链表长度为奇数时,ptr2.next可能为null;

当链表长度为偶数时,ptr2可能为null。

所以把这两个情况作为终止条件,在while判断就好,最后返回fakehead.next。

代码如下:

 1   public ListNode swapPairs(ListNode head) {
 2       if(head == null || head.next == null)
 3         return head;
 4     
 5       ListNode fakehead = new ListNode(-1);
 6       fakehead.next = head;
 7       
 8       ListNode ptr1 = fakehead;
 9       ListNode ptr2 = head;
       
       while(ptr2!=null && ptr2.next!=null){
           ListNode nextstart = ptr2.next.next;
           ptr2.next.next = ptr2;
           ptr1.next = ptr2.next;
           ptr2.next = nextstart;
           ptr1 = ptr2;
           ptr2 = ptr2.next;
       }
     return fakehead.next;
   }

Reference://http://gongxuns.blogspot.com/2012/12/leetcodeswap-nodes-in-pairs.html

Swap Nodes in Pairs leetcode java的更多相关文章

  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

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

  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 Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  7. LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation

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

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

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

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

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

随机推荐

  1. JavaScript 网页脚本语言 由浅入深 (随笔)

    1)基础 学习目的: 1. 客户端表单验证 2. 页面动态效果 3. jQuery的基础 什么是JavaScript? 一种描述性语言,也是一种基于对象和事件驱动的,并具有安全性能的脚本语言 java ...

  2. count 【mysql】

    如果你的需要是统计总行数时,为什么要使用count(*),而避免使用指定具体的列名? count()函数里面的参数是列名的的时候,那么会计算这个字段有值项的次数.也就是,该字段没有值的项并不会进入计算 ...

  3. 机器学习之路: tensorflow 一个最简单的神经网络

    git: https://github.com/linyi0604/MachineLearning/tree/master/07_tensorflow/ import tensorflow as tf ...

  4. 机器学习之路: python线性回归 过拟合 L1与L2正则化

    git:https://github.com/linyi0604/MachineLearning 正则化: 提高模型在未知数据上的泛化能力 避免参数过拟合正则化常用的方法: 在目标函数上增加对参数的惩 ...

  5. Where should we fork this repository?

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 我们应该在哪里分叉这个存储库? Where should we fork this re ...

  6. UOJ275 组合数问题

    给定n,m和k,求有多少对(i , j)满足0 ≤ i ≤ n, 0 ≤ j ≤ min(i ,m)且C(︀i,j)︀是k的倍数.n,m ≤ 1018, k ≤ 100,且k是质数. 把i和j都看成k ...

  7. 2017-2018-1 20162307 Dijkstra算法

    2017-2018-1 20162307 Dijkstra算法 题目要求 Dijkstra算法,求解附图顶点A的单源最短路径 在纸上画出求解过程,上传截图(注意图上要有自己的学号和姓名) 解题步骤

  8. hdu 5288 OO’s Sequence(2015多校第一场第1题)枚举因子

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 题意:在闭区间[l,r]内有一个数a[i],a[i]不能整除 除去自身以外的其他的数,f(l,r ...

  9. Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs

    B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...

  10. Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...