Leetcode 24 Swap Nodes in Pairs

2019-04-22  本文已影响0人  Mereder

题目描述

Given a linked list, swap every two adjacent nodes and return its head.

You may not modify the values in the list's nodes, only nodes itself may be changed.

Example:

Given 1->2->3->4, you should return the list as 2->1->4->3.

解题思路

tail p q 三个指针

tail 表示 交换过的链表

p表示待交换第一个 q为第二个,即一次操作 交换p和q。

交换过p和q之后再移动 tail

题解

    public ListNode swapPairs(ListNode head) {
        if(head == null) return null;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode p = head;
        ListNode q = head.next;
        ListNode tail = dummy;
        while(p!= null && q!=null){
            // swap
            p.next = q.next;
            q.next = p;
            tail.next = q;
            // 移动tail
            tail = p;
            p = tail.next;
            if(p == null) break;
            q = p.next;
        }
        return dummy.next;
    }
上一篇 下一篇

猜你喜欢

热点阅读