当前位置: 首页 > news >正文

旋转链表-双指针思想-LeetCode61

题目要求:给定链表的头结点,旋转链表,将链表每个节点向右移动K个位置。
示例:
输入:head = [1,2,3,4,5], k=2
输出:[4,5,1,2,3]
在这里插入图片描述

双指针思想:
先用双指针策略找到倒数K的位置,也就是(1,2,3)和4,5)两个序列,之后再将两个链表拼接成(4,5,1,2,3}就行了。
具体思路是:
因为k有可能大于链表长度,所以首先获取一下链表长度len,如果然后k=k % len,如果k == 0,则不用旋转,直接返回头结点。否则:
1、快指针先走k步
2、慢指针和快指针一起走
3、快指针走到链表尾部时,慢指针所在位置刚好是要断开的地方。把快指针指向的节点连到原链表头部,慢指针指向的节点断开和下一节点的联系
4、返回结束时慢指针指向节点的下一节点

import java.util.*;public class RotateRight_旋转数组 {public static void main(String[] args) {//int[] a = {1, 2, 3, 4, 5};ArrayList<Integer> lst = new ArrayList<>();//输入Scanner scanner = new Scanner(System.in);String s = scanner.nextLine();Scanner input = new Scanner(s);while(input.hasNextInt()){lst.add(input.nextInt());}Integer[] a = lst.toArray(new Integer[lst.size()]);ListNode nodeA = initLinkedList(a); //数组初始化为链表ListNode nodeB = initLinkedList2(lst); //集合初始化为链表ListNode node = rotateRight(nodeB, 2);  //开始旋转System.out.println(toString(node));}//定义链表节点static class ListNode{public int val;public ListNode next;ListNode(int x){val = x;next = null;}}//数组初始化链表public static ListNode initLinkedList(Integer[] a){ListNode head = null, cur = null;for (int i = 0; i < a.length; i++){ListNode newNode = new ListNode(a[i]);if (i==0){head = newNode;cur = newNode;}else{cur.next = newNode;cur = cur.next;}}return head;}//集合初始化链表public static ListNode initLinkedList2(ArrayList a){ListNode head = null, cur = null;for (int i = 0; i < a.size(); i++){ListNode newNode = new ListNode((Integer) a.get(i));if (i==0){head = newNode;cur = newNode;}else{cur.next = newNode;cur = cur.next;}}return head;}//开始旋转public static ListNode rotateRight(ListNode head, int k) {if (head == null || k == 0) {return head;}ListNode temp = head;ListNode fast = head;ListNode slow = head;int len = 0;//链表的长度while (head != null) {head = head.next;len++;}//如果能整除,则直接返回该链表if (k % len == 0) {return temp;}while ((k % len) > 0) {k--;fast = fast.next;}while (fast.next != null) {fast = fast.next;slow = slow.next;}ListNode res = slow.next;slow.next = null;fast.next = temp;return res;}//输出链表public static String toString(ListNode head) {ListNode current = head;//StringBuilder可以用来拼接字符串StringBuilder sb = new StringBuilder();while(current !=null){sb.append(current.val).append("\t");current = current.next;}return sb.toString();}}
http://www.lryc.cn/news/170306.html

相关文章:

  • 使用自定义XML配置文件在.NET桌面程序中保存设置
  • 1787_函数指针的使用
  • 解决nomachine扫描不出ip问题
  • Web 3.0 发展到什么水平了?
  • 大模型:如何利用旧的tokenizer训练出一个新的来?
  • 【LeetCode-中等题】107. 二叉树的层序遍历 II
  • 斯坦福联合培养博士|专科生的逆袭之路
  • Verilog中parameter在仿真时的应用
  • v-model绑定导致的element UI文本框输入第一次值后被绑定,导致空文本框无法再输入文字
  • 数据结构——KD树
  • python趣味编程-恐龙克隆游戏
  • 【漏洞复现】泛微e-office OfficeServer2.php 存在任意文件读取漏洞复现
  • 基于Yolov8的野外烟雾检测(4):通道优先卷积注意力(CPCA),效果秒杀CBAM和SE等 | 中科院2023最新发表
  • 程序员必掌握的核心算法:提升编程技能的关键路径
  • 面试算法10:和为k的子数组
  • 王道考研操作系统
  • HEXO 基本使用
  • Webpack Sourcemap文件泄露漏洞
  • WebGL层次模型——单节点模型
  • 【链表】反转链表 II-力扣 92 题
  • 【考研数学】高等数学第六模块 —— 空间解析几何(1,向量基本概念与运算)
  • 巨人互动|Facebook海外户Facebook客户反馈分数
  • Tomcat多实例部署和动静分离
  • 关于 C/C++ 中在指针前加 const 关键字的作用说明
  • Vue.js新手指南:从零开始建立你的第一个应用
  • 【案例】--EasyExcel导入导出文件案例
  • 深入探索图像处理:从基础到高级应用
  • Jetpack Compose基础组件 - Image
  • UINavigationController内的页面跳转实现 UIViewController 的 present和dismiss动画
  • PMP对项目管理工作有什么用?