美文网首页
Leetcode 24 Swap Nodes in Pairs

Leetcode 24 Swap Nodes in Pairs

作者: Mereder | 来源:发表于2019-04-22 19:44 被阅读0次

题目描述

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;
    }

相关文章

网友评论

      本文标题:Leetcode 24 Swap Nodes in Pairs

      本文链接:https://www.haomeiwen.com/subject/tugugqtx.html