`
473687880
  • 浏览: 484204 次
文章分类
社区版块
存档分类
最新评论

Swap Nodes in Pairs @LeetCode

 
阅读更多

判断比较花时间,需要分奇数个节点和偶数个节点的情况


package Level2;

import Utility.ListNode;

/**
 * Swap Nodes in Pairs 
 * 
 * 	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.
 *
 */
public class S24 {

	public static void main(String[] args) {
		ListNode n1 = new ListNode(1);
		ListNode n2 = new ListNode(2);
		n1.next = n2;
		ListNode n3 = new ListNode(3);
		n2.next = n3;
		n1.print();
		ListNode head = swapPairs(n1);
		head.print();
	}
	
	public static ListNode swapPairs(ListNode head) {
        if(head == null){
        	return null;
        }
        // 当只有一个元素的情况
        if(head.next == null){
        	return head;
        }
        ListNode i = head;	// i指向第1个
        ListNode j = i.next;	// j指向第2个
        ListNode k = j.next;	// k指向第3个
        
        head = head.next;	
        while(j != null){
        	j.next = i;
        	if(k!=null && k.next!=null){	// 当有偶数个节点 
        		i.next = k.next;
        	}else{		// 当有奇数个节点
        		i.next = k;
        	}
        	
        	// 更新i,j,k的值,前进两格
        	i = k;
        	if(k != null){
        		j = k.next;
        	}else{
        		j = null;
        	}
        	if(k!=null && k.next!=null){
        		k = k.next.next;
        	}else{
        		k = null;
        	}
        }
        return head;
	}
}


分享到:
评论

相关推荐

    程序员面试宝典LeetCode刷题手册

    第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays...24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 26. Remove Dupli

    多线程leetcode-leetcode-java:leetcode上的题解,基于java语言

    Swap Nodes in Pairs Spiral Matrix Path Sum II Copy List with Random Pointer Building H2O Fizz Buzz Multithreaded hard Merge k Sorted Lists Reverse Nodes in k-Group Trapping Rain Water

    leetcode下载-algorithm-1:力扣、HDU、ZOJ、POJ

    leetcode下载 Algorithm 每日一题 && 天天进步一点点 题目来于 LeetCode,剑指offer,Coding Interview,ZOJ,POJ 等平台。 欢迎Coders对代码加以指正和提议!...Nodes in Pairs 练习: leetcode: 237. D

    LeetCode最全代码

    421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...

    push_swap:push_swap 2021 @ 42

    push_swap:push_swap 2021 @ 42

    leetcode296-leetcode-in-py-and-go:Go中的Leetcode

    24:swap-nodes-in-pairs 漂亮的递归解决方案 25: reverse-nodes-in-k-group: 解析 pre_for_next 到辅助函数 29:除以两个整数:溢出; 两反 31:下一个排列:再做一次(排序!) 32:最长有效(),使用栈,左推idx ...

    swap_1位swap电路_logisim_swap_

    swap电路:当输入c=0时,输出x等于输入a,输出y等于输入b。当输入c=1时,则交换两输出,即输出y等于输入a

    leetcode添加元素使和等于-leetcode:力码

    leetcode添加元素使和等于 总结 按照类别分类来刷 刷当前题的时候,看下『题目描述』...swap-nodes-in-pairs linked-list-cycle linked-list-cycle-ii reverse-nodes-in-k-group 二叉树 实现一个二叉树 二叉树二叉树的

    leetcode打不开-leetcode:leetcode

    leetcode打不开Leetcode Note Tips Tip1: Two pointer for sorted array (#Array 1. Two Sum) Tip2: Sum[i:j] = Sum[0:j] - Sum[0:i] for continuous array (# Array 560. Subarray Sum Equals K) Tip3: Knapsack ...

    swap_swap_

    swap logisim emmm 电路

    Dir-For-LeetCode

    024_Swap_Nodes_in_Pairs 025_Reverse_Nodes_in_k-Group 026_Remove_Duplicates_from_Sorted_Array 027_Remove_Element 028_Implement_strStr() 029_Divide_Two_Integers 030_Substring_with_Concatenation...

    LINUX 查看进程占用swap

    用于查看LINUX下进程占用SWAP大小

    faceSwap环境配置全套资源

    用于faceswap源码所需要的全套安装包,包含已经配置好的python,设置环境变量既可以用

    修改swap分区大小方法

    修改swap分区大小方法,如果安装完linux后感觉swap分区不够用,可以尝试此方法。

    增大swap分区.txt 系统安装后修改swap分区

    增大swap分区.txt增大swap分区.txt增大swap分区.txt增大swap分区.txt增大swap分区.txt增大swap分区.txt增大swap分区.txt增大swap分区.txt增大swap分区.txt

    Linux系统Swap交换区

    Swap的调整对Linux服务器,特别是Web服务器的性能至关重要。通过调整Swap,有时可以越过系统性能瓶颈,节省系统升级费用。Swap空间的作用可简单描述为:当系统的物理内存不够用的时候,就需要将物理内存 中的一部分...

    SUSE上扩容Swap空间方法

    SUSE上扩容Swap空间方法,让你真正体验Linux系统的强大吧。

    windows下浏览ext、swap分区

    windows下浏览ext、swap分区

    利用swap文件的方式增加虚拟的swap空间

    如果你的硬盘空间已经全部分配给其他分区,也没有多余的预算新添购硬盘,可以利用swap文件的方式增加虚拟的swap空间,不过执行性能会较实际的swap分区差。

    FaceSwap_python_faceswap_

    使用python完成的faceswap程序,使用单张图片完成对人脸的三维建模

Global site tag (gtag.js) - Google Analytics