力扣24 两两交换链表中的节点

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

示例 1:

输入:head = [1,2,3,4]
输出:[2,1,4,3]

示例 2:

输入:head = []
输出:[]

示例 3:

输入:head = [1]
输出:[1]

方法:迭代

思路与算法

创建哑结点 dummy,令 dummy.next = head。令p表示当前到达的节点,初始时 p = dummy。每次需要交换 temp 后面的两个节点。

如果 p的后面没有节点或者只有一个节点,则没有更多的节点需要交换,因此结束交换,结束条件就是此句代码( while(p.next!=null && p.next.next!=null))。否则,获得 p后面的两个节点 node1 和 node2,通过更新节点的指针关系实现两两交换节点。

核心代码模式:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {
        while(head == null || head.next ==null){
            return head;
        }
        //辅助dummy节点
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode p = dummy;
        while(p.next!=null && p.next.next!=null){
            ListNode n1 = p.next;
            ListNode n2 = p.next.next;
            p.next = n2; //p指向2
            n1.next = n2.next; //1指向3
            n2.next = n1; //2指向1
            p = n1; //p去到1的位置
        }
        return dummy.next;
    }
}

ACM代码模式:

public class Solution24 {
    public static class ListNode{
        int val;
        ListNode next;

        public ListNode() {
        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

    }

    //1.定义创建链表方法(头插法)
    public static ListNode createList(int[] arr){
        while (arr.length != 0){
            ListNode head = new ListNode(arr[0]);
            ListNode tail = head;
            for (int i = 1; i < arr.length; i++){
                ListNode newNode = new ListNode(arr[i]);
                tail.next = newNode;
                tail = newNode;
            }
            return head;
        }
        ListNode head = null;
        return head;
    }

    //2.打印链表
    public static void printList(ListNode head){
        System.out.print("[");
        while (head!=null){
            if(head.next == null){
                System.out.print(head.val);
            }else {
                System.out.print(head.val+",");
            }
            head = head.next;
        }
        System.out.print("]");
    }
    //3.实现节点互换的算法
    public static ListNode swapPairs(ListNode head) {
        while (head==null || head.next==null){
            return head;
        }
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode p = dummy;
        while (p.next != null && p.next.next != null){
            ListNode n1 = p.next;
            ListNode n2 = p.next.next;
            p.next = n2;
            n1.next = n2.next;
            n2.next = n1;
            p = n1;
        }
        return dummy.next;
    }

    public static void main(String[] args){
        int[] arr = {1,2,3,4};
        ListNode list = createList(arr);
        ListNode ansList = swapPairs(list);
        printList(ansList);
    }
}

 结果:[2,1,4,3]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值