Leetcode-23 Swap Nodes in Pairs (java)

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

翻译

给定一个链表,交互相邻的两个节点,并返回链表的头部。
比如:给定链表1->2->3->4, 应该返回2->1->4->3
应当使用常量空间时间,不能修改节点的值,但是可以修改节点本身。

解题思路

这道题目有两个限定条件:
1、使用常量空间实现;2、不能修改节点的值。
所以声明一个节点为ln(0),其next指向head, temp节点为临时节点。
ln.next表示第一个节点,ln.next.next 标识第二个节点。
现在需要的工作就是讲ln.next, ln.next.next 交互位置。
通过下面方法来实现ln.next 和 ln.next.next 位置的呼唤。

      temp = ln.next;
      ln.next = ln.next.next;
      temp.next = ln.next.next;
      ln.next.next = temp;     

时间复杂度为O(n),
空间夫再度为O(2);

代码示例-Java

/**
 * Created by yumodev on 9/13/16.
 */
public class SwapPairs_24 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public static ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null){
            return head;
        }

        int n = 1;

        ListNode ln = new ListNode(0);
        ln.next = head;
        head = head.next;
        ListNode temp;
        while (true){
            if (n % 2 == 0){
                temp = ln.next;
                ln.next = ln.next.next;
                temp.next = ln.next.next;
                ln.next.next = temp;
                ln = ln.next.next;
            }

            if (ln.next == null || ln.next.next == null){
                break;
            }

            n++;
        }

        return head;
    }

    public static ListNode initListNode(int num) {
        int mod = 0;
        int temp = num;
        ListNode ln = null;
        ListNode nextLn = null;
        while (temp > 0) {
            mod = temp % 10;
            temp = temp / 10;
            if (ln == null){
                nextLn = ln = new ListNode(mod);
            }else{
                nextLn.next = new ListNode(mod);
                nextLn = nextLn.next;
            }
        }

        return ln;
    }

    public static String ListNodeToString(final ListNode listNode) {
        StringBuilder sb = new StringBuilder();
        ListNode ln = listNode;
        while (ln != null) {
            sb.append(ln.val + " ");
            ln = ln.next;
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        ListNode l1 = initListNode(4321);
        //ListNode l1 = initListNode(321);
        long startTime = System.nanoTime();
        ListNode l2 = swapPairs(l1);
        long endTime = System.nanoTime();
        long time = endTime - startTime;

        System.out.println("removeNthFromEnd:" + ListNodeToString(l2) + " time:" + time);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值