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;
* next = null;
* }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
if(head==null||head.next==null)
return head;
ListNode faked=new ListNode(0);
faked.next=head;
ListNode pre=faked;
ListNode cur=head;
while(cur!=null&&cur.next!=null){
pre.next=cur.next;
cur.next=pre.next.next;
pre.next.next=cur;
pre=cur;
cur=pre.next;
}
return faked.next;
}
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

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

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

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

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

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

  3. 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 ...

  4. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  5. LeetCode 024 Swap Nodes in Pairs

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

  6. [LeetCode] 24. Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  7. 【leetcode】Swap Nodes in Pairs (middle)

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

  8. Java for LeetCode 024 Swap Nodes in Pairs

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

  9. leetcode:Swap Nodes in Pairs

    Given a linked list, swap every two adjacent(相邻的) nodes and return its head. For example,Given 1-> ...

随机推荐

  1. 实习第一天之数据绑定:<%#Eval("PartyName")%>'

    1.asp:HyperLink ID="Link" runat="server" Target="_blank" Text='<%#E ...

  2. java解析String类型t复杂xml,多级节点,最好的例子

    需要用jar包 dom4j-1.6.1.jar 字符串xml如下: <root> <flw> <name>aa</name> <age>22 ...

  3. Java调用cmd压缩文件

    今天在做一个java调用windows的压缩命令时遇到一奇怪问题代码如下: String cmd ="C:/Program Files (x86)/WinRAR/rar.exe a c:/t ...

  4. 再探vim经常使用命令

     最開始学习过vim,见 http://blog.csdn.net/u011848617/article/details/12837873 之后以前不了了之,当再次学习后,发现经常使用命令的掌握还 ...

  5. linux下Oracle11g RAC搭建(九)

    linux下Oracle11g RAC搭建(九) 八.创建ASM仓储 相同在图形化界面操作 [root@node1 ~]#  su - grid [grid@node1 ~]$ asmca   //创 ...

  6. Oracle SQL Lesson (2) - 限制和排序数据

    重建scott用户@?/rdbms/admin/utlsampl.sql@--执行?--$ORACLE_HOME 字符区分大小写:SELECT last_name, job_id, departmen ...

  7. Operand should contain 1 column(s)

    今天sql当测试发现错误:Operand should contain 1 column(s). 因为in背后有多种条件字段,in只有有背后场.

  8. Hadoop-2.2.0中国文献——MapReduce 下一代 —配置单节点集群

    Mapreduce 包 你需从公布页面获得MapReduce tar包.若不能.你要将源代码打成tar包. $ mvn clean install -DskipTests $ cd hadoop-ma ...

  9. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

  10. [Ext.Net]GridPanel之Access数据库分页显示

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...