Question

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.

Solution

这题的核心在于dummy node和list操作。

dummy -> 1 -> 2 -> 3 -> 4

prev   cur   next  tmp

我们用四个指针来完成操作。

1. 预存tmp: tmp = next.next

2. 更改next: next.next = cur

3. 更改cur: cur.next = tmp

4. 更改prev: prev.next = next

5. 更新prev, cur, next:

cur = tmp

if (cur != null): next = cur.next

prev = prev.next.next

最后返回dummy.next

我们看到循环结束的条件应该是tmp为null或者tmp.next为null.

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode prev = dummy, current = head, next = head.next, tmp;
while (next != null) {
tmp = next.next;
next.next = current;
current.next = tmp;
prev.next = next;
current = tmp;
prev = prev.next.next;
if (current == null) {
break;
} else {
next = current.next;
}
}
return dummy.next;
}
}

Swap Nodes in Pairs 解答的更多相关文章

  1. [LeetCode]Swap Nodes in Pairs题解

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

  2. Leetcode-24 Swap Nodes in Pairs

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

  3. 24. Swap Nodes in Pairs

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

  4. [LintCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head.   Example Given 1->2-> ...

  5. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

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

  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][Python]24: Swap Nodes in Pairs

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

  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. zoj3640:概率(期望)dp

    题目大意:有一个吸血鬼,初始攻击力为f,每天随机走到n个洞里面,每个洞有一个c[i],如果他的攻击力f>c[i] 则可以花费t[i] 的时间逃走,否则则花费一天时间使自己的攻击力增加c[i],求 ...

  2. Linux 内存管理知识学习总结

    现在的服务器大部分都是运行在Linux上面的,所以,作为一个程序员有必要简单地了解一下系统是如何运行的.对于内存部分需要知道: 地址映射 内存管理的方式 缺页异常 先来看一些基本的知识,在进程看来,内 ...

  3. [RxJS] Handling Multiple Streams with Merge

    You often need to handle multiple user interactions set to different streams. This lesson shows hows ...

  4. oracle之case when

    oracle case when 的用法 http://www.cnblogs.com/xiaowu/archive/2011/08/17/2143445.html(转) http://www.cnb ...

  5. iOS中AutoLayer自动布局流程及相关方法【转】

    转自:http://my.oschina.net/w11h22j33/blog/208574 关于UIView的Layer,IOS提供了三个方法: 1.layoutSubviews 在iOS5.1和之 ...

  6. 先装Net Framework 后 装 IIS的处理办法

    先装IIS话,后面装Net Framework时候会自动注册 处理aspx和ashx等的处理扩展程序 先装Net Framework 后 装 IIS.扩展程序注册在命令:aspnet_regiis - ...

  7. 网站发布在另外一个网站下面配置伪静态之后图片样式和JS丢失

    <script src="<%=ResolveClientUrl("~/content/js/jquery-1.7.1.min.js") %>" ...

  8. iOS_SN_push/pop转场动画封装和一般动画封装

    封装类中的方法: #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface AnimationE ...

  9. c#按键Up和Down对Textbox的内容加1减1

    private void textBox_proc1value_KeyDown(object sender, KeyEventArgs e) { Keys key = e.KeyCode; if (e ...

  10. VC中窗口ID,句柄,指针三者相互转换函数【转】

    ID--HANDLE--HWND三者之间的互相转换id->句柄        hWnd = ::GetDlgItem(hParentWnd,id);id->指针        CWnd:: ...