【Hot100】LeetCode—24. 两两交换链表中的节点

王柳  论坛元老 | 2024-8-22 13:41:34 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1024|帖子 1024|积分 3072




  • 原题毗连:24. 两两交换链表中的节点

1- 思路

四指针



  • 界说 dummyHead:便于处理惩罚头结点
  • ① cur 指针,记录两个交换节点的前 前一个结点
  • ② 第一个指针 first
  • ③ 第二个指针 second
更新:


  • 初始化 first 和 second :first = cur.next、second = cur.next.next
  • 更新:结点翻转后跟新,涉及三个步调



  • first 的 next 更新 ——> ①
  • second 的 next 更新 ——> ②
  • 交换后前一个结点的 next 更新 ——> ③

2- 实现

⭐24. 两两交换链表中的节点——题解思路


  1. class Solution {
  2.     public ListNode swapPairs(ListNode head) {
  3.         ListNode dummyHead = new ListNode(-1);
  4.         dummyHead.next = head;
  5.         ListNode cur = dummyHead;
  6.         ListNode first = null;
  7.         ListNode second = null;
  8.         while(cur.next!=null && cur.next.next != null){
  9.             first = cur.next;
  10.             second = cur.next.next;
  11.             first.next = second.next;
  12.             second.next = first;
  13.             cur.next = second;
  14.             cur = first;
  15.         }
  16.         return dummyHead.next;
  17.     }
  18. }
复制代码

3- ACM 实现

  1. public class swapPairs {
  2.     public static class ListNode {
  3.         int val;
  4.         ListNode next;
  5.         ListNode(int x) {
  6.             val = x;
  7.             next = null;
  8.         }
  9.     }
  10.     public static ListNode swapPairs(ListNode head){
  11.         // 1. 数据结构
  12.         ListNode dummyHead = new ListNode(-1);
  13.         dummyHead.next = head;
  14.         ListNode cur = dummyHead;
  15.         ListNode first = null;
  16.         ListNode second = null;
  17.         // 2.循环
  18.         while(cur.next!=null && cur.next.next!=null){
  19.             first = cur.next;
  20.             second = cur.next.next;
  21.             //交换
  22.             first.next = second.next;
  23.             second.next = first;
  24.             cur.next = second;
  25.             cur = first;
  26.         }
  27.         return dummyHead.next;
  28.     }
  29.     public static void main(String[] args) {
  30.         Scanner sc = new Scanner(System.in);
  31.         int n1 = sc.nextInt();
  32.         ListNode head1 = null, tail1 = null;
  33.         for (int i = 0; i < n1; i++) {
  34.             int val = sc.nextInt();
  35.             ListNode newNode = new ListNode(val);
  36.             if (head1 == null) {
  37.                 head1 = newNode;
  38.                 tail1 = newNode;
  39.             } else {
  40.                 tail1.next = newNode;
  41.                 tail1 = newNode;
  42.             }
  43.         }
  44.         ListNode forRes = swapPairs(head1);
  45.         while(forRes!=null){
  46.             System.out.print(forRes.val+" ");
  47.             forRes = forRes.next;
  48.         }
  49.     }
  50. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王柳

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表