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.

解题思路:

感觉和前几题很类似,直接实现即可,JAVA代码如下:

public ListNode swapPairs(ListNode head) {
ListNode result = new ListNode(0), index = result;
if (head == null)
return result.next;
while (head.next != null) {
index.next = new ListNode(head.next.val);
index = index.next;
index.next = new ListNode(head.val);
index = index.next;
head = head.next.next;
if (head == null)
return result.next;
}
index.next = new ListNode(head.val);
return result.next;
}

Java for LeetCode 024 Swap Nodes in Pairs的更多相关文章

  1. LeetCode 024 Swap Nodes in Pairs

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

  2. LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点

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

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

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

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

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

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

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

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

  7. LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java

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

  8. Java [leetcode 24]Swap Nodes in Pairs

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

  9. 【LeetCode】024. Swap Nodes in Pairs

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

随机推荐

  1. 洛谷P1130 红牌

    题目描述 某地临时居民想获得长期居住权就必须申请拿到红牌.获得红牌的过程是相当复杂 ,一共包括N个步骤.每一步骤都由政府的某个工作人员负责检查你所提交的材料是否符合条件.为了加快进程,每一步政府都派了 ...

  2. poj1631Bridging signals(最长单调递增子序列 nlgn)

    Bridging signals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12251   Accepted: 6687 ...

  3. 栈的的链式实例LinkStack实现

    1.#include <stdio.h>#include <malloc.h>#include "LinkList.h"typedef struct _ta ...

  4. Note:JSON

    JSON是JavaScript的原生格式,意味着在JavaScript中处理JSON数据不需要特殊的API或工具包,它是完全独立于语言的文本格式,可以把JavaScript对象中的一组对象转化为字符串 ...

  5. IF IE

    1. <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]-->2. <!--[if IE]> 所有的IE可识别 & ...

  6. vsftpd 搭建与介绍

    CentOS Linux Vsftp服务器配置 CentOS Linux Vsftp服务器配置 1.开启防火墙ftp端口           vi /etc/sysconfig/iptables    ...

  7. javascript正则——贪婪匹配

    熟悉正则的朋友都知道,正则的匹配有“贪婪”和“非贪婪”之分. “贪婪”匹配是尽可能多的匹配: 对于字符串‘aaaa’, /a+/匹配整个字符串,而非贪婪匹配/a+?/匹配的是整个字符串的第一个‘a’, ...

  8. 【分享】4款WiFi广告营销利器推荐

    随着WiFi的普及,相信大家已经发现很多公共场合连接WiFi的时候会要求登录,这个就是所谓的WiFi广告了. 什么是WiFi广告呢?(以下引用百科)       WiFi广告是目前WiFi营销的主要方 ...

  9. excel插入当前时间快捷键Ctrl+;

    之前写了一篇editplus如何插入当前时间_Ctrl+D的文章,有的同学说excel用习惯了,那在这我们就说一下excel插入当前时间快捷键,让您在excel快速插入当前时间 excel插入当前时间 ...

  10. Robberies

     Robberies Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit S ...