CODE 72: Rotate List

本文详细介绍了如何实现一个链表的右移操作,包括关键步骤和算法逻辑,通过实例演示了具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a list, rotate the list to the right by k places, where k is non-negative.

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

	public ListNode rotateRight(ListNode head, int n) {
		// Note: The Solution object is instantiated only once and is reused by
		// each test case.
		if (null == head) {
			return null;
		}
		ListNode round1 = null;
		ListNode p = head;
		int length = 0;
		while (null != p) {
			ListNode node = new ListNode(p.val);
			node.next = round1;
			p = p.next;
			round1 = node;
			length++;
		}
		if (n % length == 0) {
			return head;
		} else {
			n = n % length;
		}
		int num = 0;
		ListNode round2 = null;
		ListNode p1 = round1;
		while (num < n) {
			ListNode node = new ListNode(p1.val);
			node.next = round2;
			p1 = p1.next;
			round2 = node;
			num++;
		}

		ListNode round3 = new ListNode(0);
		ListNode round3Cpy = round3;
		ListNode p2 = round2;
		while (null != p2) {
			ListNode node = new ListNode(p2.val);
			p2 = p2.next;
			round3Cpy.next = node;
			round3Cpy = round3Cpy.next;
		}
		while (null != p1) {
			ListNode node = new ListNode(p1.val);
			node.next = round3Cpy.next;
			round3Cpy.next = node;
			p1 = p1.next;
		}
		return round3.next;
	}


<template> <div> <div class="alarm-type-container" v-if="selectList.length"> <a-input-search :placeholder="$t('请输入报警类型名称')" @search="search" style="width: 100%;margin-bottom: 15px;" /> <template v-if="listShow"> <template v-for="(item, index) in selectList"> <div class="allCheckBox" v-if="item.count != 0" :key="item.id"> <div class="allCheck"> <a-checkbox :indeterminate="item.indeterminate" :checked="item.checkAll" @change="checked => onCheckAllChange(checked, index)">{{ $t(item.alarmBigTypeName) }}</a-checkbox> <img class="down" @click="setHeight(index)" :class="item.flag ? 'up' : ''" src="~@/assets/img/down.png" alt="" /> </div> <div :class="['checkboxGroupList', { 'checkboxUp': item.flag, 'checkboxDown': !item.flag }]"> <a-checkbox-group :value="item.codes" @change="checked => onChange(checked, index)"> <template v-for="check in item.alarmTypeVoList"> <a-checkbox :key="check.alarmTypeCode" v-if="!check.show" :value="check.alarmTypeCode"> <span :title="$t(check.alarmTypeName)"> {{ $t(check.alarmTypeName) }} </span> </a-checkbox> </template> </a-checkbox-group> </div> </div> </template> </template> <noData v-else /> </div> <div class="alarm-type-container" style="display: flex;justify-content: center;" v-else> <a-space :size="64"> <a-spin tip="加载中,请稍候..." /> </a-space> </div> </div> </template> <script> import moment from "moment"; import noData from '@/components/tableNoData' export default { name: "alarm-type-container", components: { noData }, props: { visible: { type: Boolean, default: () => false }, selectData: { type: Array, default: () => { return []; } }, isTemplate: { type: Boolean, default: () => false } }, data() { return { selectList: [], searchValue: '', codeList: [], listShow: true, checkCodes: [], delList: [], seatchType: false, selectListBak: [] }; }, watch: { }, created() { }, methods: { moment, deWeightCode(tempArr) { let newArr = []; for (let i = 0; i < tempArr.length; i++) { if (newArr.indexOf(tempArr[i]) == -1) { newArr.push(tempArr[i]); } else { tempArr.splice(i, 1); } } return tempArr; }, search(val, e) { if (val) { this.listShow = false//是否有数据 this.selectList.forEach((item, index) => { item.count = item.alarmTypeVoList.length item.alarmTypeVoList.forEach(select => { //控制显示隐藏 if (select.alarmTypeName.indexOf(val) > -1) { this.listShow = true select.show = false } else { item.count = item.count - 1 select.show = true } }) if (this.selectList.length > this.codeList.length) { this.codeList.push({ alarmBigTypeCode: item.alarmBigTypeCode, codes: item.codes ? item.codes : [], showCodes: [], showLength: [] }) } if (item.codes) { this.codeList[index].showCodes = [] if (item.alarmBigTypeCode == this.codeList[index].alarmBigTypeCode) { this.codeList[index].codes = [...item.codes, ...this.codeList[index].codes] } const uniqueArr = Array.from(new Set(item.codes)); item.codes = uniqueArr item.checkedList = uniqueArr this.codeList[index].codes = uniqueArr this.codeList[index].showLength = item.alarmTypeVoList.filter(it => { return it.show == false }) item.alarmTypeVoList.forEach(ele => { item.codes.forEach(el => { if (!ele.show && ele.alarmTypeCode == el) { this.codeList[index].showCodes.push(el) } }) }); this.selectListBak[index] = item.codes } // 改变全选状态 item.indeterminate = !!this.codeList[index].showCodes.length && this.codeList[index].showCodes.length < this.codeList[index].showLength.length; item.checkAll = (this.codeList[index].showCodes.length === this.codeList[index].showLength.length) && this.codeList[index].showCodes.length != 0; }) this.seatchType = true } else { this.selectList.forEach((item, index) => { if (!item.codes) { item.codes = [] } item.count = item.alarmTypeVoList.length item.alarmTypeVoList.forEach(select => { select.show = false }) item.codes = [...new Set(item.codes)] item.indeterminate = !!item.codes.length && item.codes.length < item.alarmTypeVoList.length; item.checkAll = item.codes.length === item.alarmTypeVoList.length; this.selectListBak[index] = [] }) this.listShow = true this.seatchType = false this.codeList = [] } this.$forceUpdate() }, setHeight(index) { this.selectList[index].flag = !this.selectList[index].flag; this.$forceUpdate(); }, getEle() { return document.querySelector("#app>.standard-page"); }, onChange(checkedList, index) { checkedList = [...new Set(checkedList)] this.delList = [] if (this.codeList.length && this.selectList[index].codes && this.codeList[index].showCodes.length) { let oldCodes = [this.selectListBak[index]]//搜索前选中的 this.codeList[index].showCodes = [...this.codeList[index].showCodes, ...checkedList] this.codeList[index].showCodes = [...new Set(this.codeList[index].showCodes)] this.codeList[index].showCodes.forEach(items => { if (checkedList.indexOf(items) > -1) { } else { this.delList.push(items) this.delList = [...new Set(this.delList)] } }); let list = [...this.selectListBak[index].filter(e => !this.delList.includes(e)), ...checkedList] list = [...new Set(list)] this.selectList[index].checkedList = [...list] this.selectList[index].codes = [...list] } else { if (this.selectListBak[index]) { let list = [] if (this.selectListBak[index].length == this.selectList[index].alarmTypeVoList.length || !this.seatchType) { list = [...checkedList] } else { list = [...this.selectListBak[index], ...checkedList] } this.selectList[index].checkedList = [...new Set(list)] this.selectList[index].codes = [...new Set(list)] this.selectList[index].checkedList = [...new Set(list)] } else { this.selectList[index].checkedList = [...checkedList] this.selectList[index].codes = [...checkedList] } } this.selectList[index].indeterminate = !!checkedList.length && checkedList.length < this.selectList[index].alarmTypeVoList.length; this.selectList[index].checkAll = checkedList.length === this.selectList[index].alarmTypeVoList.length; this.$forceUpdate(); if (this.isTemplate) { this.onSubmit(); } }, //全选 onCheckAllChange(e, index) { Object.assign(this.selectList[index], { checkedList: e.target.checked ? [...this.selectListBak[index], ...this.dataEach(this.selectList[index].alarmTypeVoList)] : [...this.selectListBak[index].filter(e => !this.dataEach(this.selectList[index].alarmTypeVoList).includes(e))], codes: e.target.checked ? [...this.selectListBak[index], ...this.dataEach(this.selectList[index].alarmTypeVoList)] : [...this.selectListBak[index].filter(e => !this.dataEach(this.selectList[index].alarmTypeVoList).includes(e))], indeterminate: false, checkAll: e.target.checked }); if (this.isTemplate) { this.onSubmit(); } this.$forceUpdate(); }, dataEach(val) { return val.map(item => { if (!item.show) { return item.alarmTypeCode } else { return null } }) }, // 提交 onSubmit(type) { if (!this.isTemplate) { this.search() } this.selectList.forEach((select, index) => { select.list = []; if (select.codes) { const uniqueArr = Array.from(new Set(select.codes)); select.codes = uniqueArr select.checkedList = uniqueArr } select.alarmTypeVoList.forEach(item => { if (select.codes.length) { select.codes.forEach(code => { if (item.alarmTypeCode == code) { select.list.push(item); } }); } }); }); if (!type) { this.$emit("checkData", this.selectList); } }, getSelectData(data, type) { this.selectList = [] this.selectListBak = [] data.forEach(items => { items.flag = true; items.count = 1 if (type) { items.checkAll = false items.indeterminate = false items.checkedList = [] items.checkedOption = [] } else { if (items.checkedOption) {//如果有选中的数据 items.checkAll = items.checkedOption.length == items.alarmTypeVoList.length ? true : false; items.indeterminate = items.checkedOption.length && !items.checkAll ? true : false; items.checkedList = items.checkedOption.map(item => { return item; }); items.codes = items.checkedOption.map(item => { if (item) { return item.alarmTypeCode; } }); } else { //反之 items.checkAll = false items.indeterminate = false items.checkedList = [] items.checkedOption = [] } } this.selectList.push(items); this.selectListBak.push(items.codes || []); }); console.log(this.selectList, 'this.selectListthis.selectListthis.selectListthis.selectList'); } } }; </script> <style lang="less" scoped> /deep/.ant-checkbox-checked { .ant-checkbox-inner:after{ transform: rotate(45deg) scale(1) translate(-50%,-50%) translateZ(0) !important; } } .alarm-type-container { width: 100%; min-height: 400px; .allCheckBox { border: 1px solid var(--split); border-radius: 4px; margin-bottom: 10px; } .allCheck { background-color: #f0f3f7; padding: 10px 16px; display: flex; justify-content: space-between; align-items: center; .down { transition: all 0.5s; width: 13px; height: 8px; cursor: pointer; } .up { transform: rotate(180deg); } } .checkboxGroupList { transition: all 1s; width: 100%; overflow: hidden; .ant-checkbox-group { width: 100%; padding: 8px 0 8px 14px; } .ant-checkbox-group-item { padding: 10px 16px; } .ant-checkbox-wrapper { padding: 4px; margin: 0; } .active { color: #2b81f1; } .ant-checkbox-wrapper { width: 25%; line-height: 28px; border-right: 1px solid #f0f3f7; padding-left: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .ant-checkbox-wrapper:nth-child(4n) { border-right: none; } } .checkboxDown { height: 0px; } .checkboxUp { min-height: 30px; } } </style> 代码优化
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值