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.

把一个链表中的每一对节点对换(不能只换值)

/**
* 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 h = new ListNode(0);
h.next = head;
ListNode p = h; while(p.next != null && p.next.next != null){
//use t1 to track first node
ListNode t1 = p;
p = p.next;
t1.next = p.next; //use t2 to track next node of the pair
ListNode t2 = p.next.next;
p.next.next = p;
p.next = t2;
} return h.next;
}
}

解题思路:这题主要涉及到链表的操作,没什么特别的技巧,注意不要出错就好。最好加一个头结点,操作起来会很方便。

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param a ListNode
# @return a ListNode
def swapPairs(self, head):
if head == None or head.next == None:
return head
dummy = ListNode(0); dummy.next = head
p = dummy
while p.next and p.next.next:
tmp = p.next.next
p.next.next = tmp.next
tmp.next = p.next
p.next = tmp
p = p.next.next
return dummy.next

22 Swap Nodes in Pairs的更多相关文章

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

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

  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. Django之auth模块(用户认证)登陆组件

    auth模块简介 auth模块是对登录认证方法的一种封装,之前我们获取用户输入的用户名及密码后需要自己从user表里查询有没有用户名和密码符合的对象, 而有了auth模块之后就可以很轻松的去验证用户的 ...

  2. poj2083 分形(图形的递归)

    题目传送门 代码有注释. #include<iostream> #include<algorithm> #include<cstdlib> #include< ...

  3. matplolib实例之 城市气候与海洋的关系研究

  4. buildKibanaServerUrl

    private String buildKibanaServerUrl(DiscountIndexMailData mailData,Statistic stat,String failureCaus ...

  5. Tcpdump命令抓包详细分析

    1 起因 前段时间,一直在调线上的一个问题:线上应用接受POST请求,请求body中的参数获取不全,存在丢失的状况.这个问题是偶发性的,大概发生的几率为5%-10%左右,这个概率已经相当高了.在排查问 ...

  6. Python中的None与 NULL(即空字符)的区别

    None是Python的特殊类型,NoneType对象,它只有一个值None. 它不支持任何运算也没有任何内建方法. None和任何其他的数据类型比较永远返回False. None有自己的数据类型No ...

  7. oracle 12.1.0.2的mgmt 导致的ORA-01017 bug

    两节点12c RAC,在两节点上export ORACLE_SID再sqlplus / as sysdba都正常登录,然而Commvault通过service_name方式(sqlplus sys/p ...

  8. js校验密码,不能为空的8-20位非纯数字或字母的密码

    jsp: <div class="mui-input-row"> <label>密码</label><!-- id='password' ...

  9. Jquery 获取table中的td元素的值

    <table id="t1"> <tr> <td> 1-1 </td> <td> 1-2 </td> < ...

  10. Unity3d C# 创建 物体

    using UnityEngine; using System.Collections; public class create : MonoBehaviour { // Use this for i ...