Swap Nodes in Pairs

简介: 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.

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.

给定一个链表,把相邻两个结点调换位置;返回head

Java代码:

 1 package com.rust.cal;
 2 
 3 /**
 4  * Definition for singly-linked list.
 5  * public class ListNode {
 6  *     int val;
 7  *     ListNode next;
 8  *     ListNode(int x) { val = x; }
 9  * }
10  */
11 public class SwapNodesinPairs {
12     public static ListNode swapPairs(ListNode head) {
13         if (head == null || head.next == null) {
14             //必须先判断head是否为null,否则会出java.lang.NullPointerException
15             //如果输入的head == null,先判断head.next会找不到目标
16             return head;
17         }
18         /* 针对前两个结点 */
19         ListNode pre = head.next, later, veryFirst;
20         head.next = pre.next;
21         pre.next = head;
22         head = pre;
23         later = head.next;
24         /* 
25          * 针对后续结点
26          * 连续有2个结点,才进行换位
27          */
28         while (later.next != null && later.next.next != null) {
29             veryFirst = later;
30             pre = pre.next.next;
31             later = later.next.next;
32             pre.next = later.next;
33             later.next = pre;
34             veryFirst.next = later;
35             later = pre;
36             pre = veryFirst.next;
37         }
38         return head;
39     }
40     
41     public static void main(String args[]){
42         /*
43          * prepare data
44          */
45         ListNode head = new ListNode(1);
46         ListNode initHead = head;
47         for (int i = 2; i < 10; i++) {
48             initHead.next = new ListNode(i);
49             initHead = initHead.next;
50         }
51         
52         head = swapPairs(head);
53         /*
54          * show data
55          */
56         ListNode newHead = head;
57         while(newHead != null){
58             System.out.print(newHead.val + "  ");
59             newHead = newHead.next;
60         }
61         ListNode nothing = new ListNode(1);
62         swapPairs(nothing.next);
63     }
64 }

 

输出:

2  1  4  3  6  5  8  7  9  

这个方法是先处理前2个结点,再循环处理后续的结点。其实结点的处理方法都差不多,在LeetCode讨论区看到递归解法,搬运过来

Java代码:

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

    ListNode n1 = head;
    ListNode n2 = head.next;

    n1.next = n2.next;
    n2.next = n1;

    n1.next = swapPairs(n1.next);

    return n2;
}

利用方法开头对head是否为null的判断作为递归的条件,比第一个方法优雅很多

目录
相关文章
|
7天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1176 3
|
6天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
877 12
|
5天前
|
机器学习/深度学习 物联网
Wan2.2再次开源数字人:Animate-14B!一键实现电影角色替换和动作驱动
今天,通义万相的视频生成模型又又又开源了!Wan2.2系列模型家族新增数字人成员Wan2.2-Animate-14B。
460 10
|
16天前
|
人工智能 运维 安全
|
7天前
|
弹性计算 Kubernetes jenkins
如何在 ECS/EKS 集群中有效使用 Jenkins
本文探讨了如何将 Jenkins 与 AWS ECS 和 EKS 集群集成,以构建高效、灵活且具备自动扩缩容能力的 CI/CD 流水线,提升软件交付效率并优化资源成本。
331 0
|
7天前
|
消息中间件 Java Apache
SpringBoot集成RocketMq
RocketMQ 是一款开源的分布式消息中间件,采用纯 Java 编写,支持事务消息、顺序消息、批量消息、定时消息及消息回溯等功能。其优势包括去除对 ZooKeeper 的依赖、支持异步和同步刷盘、高吞吐量及消息过滤等特性。RocketMQ 具备高可用性和高可靠性,适用于大规模分布式系统,能有效保障消息传输的一致性和顺序性。
405 2
|
14天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
7天前
|
云栖大会
阿里云云栖大会2025年9月24日开启,免费申请大会门票,速度领取~
2025云栖大会将于9月24-26日举行,官网免费预约畅享票,审核后短信通知,持证件入场
1227 12