资讯专栏INFORMATION COLUMN

[LintCode] Swap Nodes in Pairs

EscapedDog / 2081人阅读

摘要:指针为,我们选择的两个结点是和。要注意循环的边界条件,这两个结点不能为空。主要思路是先用和两个新结点去保存和两个结点。完成交换之后,连接和,并让前进至此时的结点。

Problem

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

Example

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

Note

指针为p,我们选择swap的两个结点是p.nextp.next.next。要注意while循环的边界条件,这两个结点不能为空。主要思路是先用nexttemp两个新结点去保存p.next.next.nextp.next两个结点。完成交换之后,连接tempnext,并让p前进至此时的temp结点。

Solution
    public class Solution {
        public ListNode swapPairs(ListNode head) {
            if (head == null) return head;
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode p = dummy;
            while (p.next != null && p.next.next != null) {
                ListNode next = p.next.next.next;
                ListNode temp = p.next;
                p.next = p.next.next;
                p.next.next = temp;
                temp.next = next;
                p = temp;
            }
            return dummy.next;
        }
    }

OR

public class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode dummy = new ListNode(0);
        dummy.next = head;        
        ListNode cur = dummy;
        while (cur.next != null && cur.next.next != null) {
            ListNode n1 = cur.next;
            ListNode n2 = cur.next.next;
            cur.next = n2;
            n1.next = n2.next;
            n2.next = n1;
            cur = n1;
        }
        return dummy.next;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65431.html

相关文章

  • [LintCode] Swap Two Nodes in Linked List

    摘要:建立结点,指向可能要对进行操作。找到值为和的结点设为,的前结点若和其中之一为,则和其中之一也一定为,返回头结点即可。正式建立,,以及对应的结点,,然后先分析和是相邻结点的两种情况是的前结点,或是的前结点再分析非相邻结点的一般情况。 Problem Given a linked list and two values v1 and v2. Swap the two nodes in th...

    wua_wua2012 评论0 收藏0
  • [Leetcode] Swap Nodes in Pairs Reverse Nodes in k-

    摘要:三指针法复杂度时间空间思路基本的操作链表,见注释。注意使用头节点方便操作头节点。翻转后,开头节点就成了最后一个节点。 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 ...

    TZLLOG 评论0 收藏0
  • leetcode24 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 r...

    GT 评论0 收藏0
  • leetcode 24 Swap Nodes in Pairs

    摘要:最后返回头节点。同时题目要求只能占用常数空间,并且不能改变节点的值,改变的是节点本身的位置。翻转是以两个节点为单位的,我们新声明一个节点表示当前操作到的位置。每次操作结束,将指针后移两个节点即可。执行操作前要确定操作的两个节点不为空。 题目详情 Given a linked list, swap every two adjacent nodes and return its head....

    heartFollower 评论0 收藏0
  • 【LC总结】翻转链表 Swap in Pairs, Reverse in k-Group, Reve

    摘要:注意这里,只要走到第位 Swap Nodes in Pairs For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Solution public class Solution { public ListNode swapPairs(ListNode head) { if...

    Steve_Wang_ 评论0 收藏0

发表评论

0条评论

EscapedDog

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<