Git Product home page Git Product logo

Comments (4)

xiongcaihu avatar xiongcaihu commented on May 4, 2024 2
/**
 * 思路,
 * 进去首先遍历一遍单链表,给每个元素加上preNode属性,表示前置结点,最后用一个变量记录尾结点
 *
 * 接下来的循环次数,应该是paseInt(len/K) + len%K
 * 分成两段:
 * 第一段循环:构造包含K个结点的逆序链表段,并拼接它们
 * 第二段循环:构造包含1个结点的逆序连标段,并拼接它们
 * 
 * @param {*} head
 * @param {*} K
 * @returns
 */
function reverseLinkedArray(head, K) {
	var len = 0;// 单链表长度
	var nowNode = head,
		preNode = null,
		tailNode = null;
	while (nowNode) {
		nowNode.preNode = preNode;
		preNode = nowNode;
		if (nowNode.next == null) {
			tailNode = nowNode;
		}
		nowNode = nowNode.next;
		len++;
	}

	var finalLinkHead = null;
	for (var i = 0, count = parseInt(len / K); i < count + (len % K); i++) {
		var j = i >= count ? 1 : K;
		var newHead = new ListNode();
		var tHead = newHead;
		while (j > 0) {
			tHead.val = tailNode.val;
			tHead.next = new ListNode();
			tailNode = tailNode.preNode;
			if (j == 1) {
				break;
			}
			tHead = tHead.next;
			j--;
		}
		tHead.next = finalLinkHead;
		finalLinkHead = newHead;
	}

	return finalLinkHead;
}

function ListNode(val) {
	this.val = null;
	this.next = null;
}

function iterateLink(link) {
	var array = [];
	while (link) {
		array.push(link.val);
		link = link.next;
	}
	return array;
}

function demo(array, K) {
	var head = new ListNode();
	var nowNode = head;
	for (var i = 0; i < array.length; i++) {
		nowNode.val = array[i];
		if (i == array.length - 1) break;
		nowNode = nowNode.next = new ListNode();
	}

	console.log(iterateLink(reverseLinkedArray(head, K)));
}

var timestart = new Date().getTime();
demo([1, 2, 3, 4, 5, 6, 7, 8], 3);
demo([0, 1, 2, 3, 4, 5, 6, 7, 8], 2);
demo([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 1);
demo([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3);
demo([0, 1], 1);
console.log("use time = ", new Date().getTime() - timestart);

from leetcode.

yjua avatar yjua commented on May 4, 2024
function reverse(arr,k){
	var len = arr.length;
	var num = len % k;
	for(var i = num; i< len; i = i+k){
		swap(i);
	}
	function swap(i){
		var temp , begin = i, end = i+k-1;
		while(end - begin > 0){
			temp = arr[begin];
			arr[begin] = arr[end];
			arr[end] =temp;
			begin ++
			end --
		}
	}
	return arr;
}

from leetcode.

azl397985856 avatar azl397985856 commented on May 4, 2024
function reverse(arr,k){
	var len = arr.length;
	var num = len % k;
	for(var i = num; i< len; i = i+k){
		swap(i);
	}
	function swap(i){
		var temp , begin = i, end = i+k-1;
		while(end - begin > 0){
			temp = arr[begin];
			arr[begin] = arr[end];
			arr[end] =temp;
			begin ++
			end --
		}
	}
	return arr;
}

是链表,不是数组

from leetcode.

azl397985856 avatar azl397985856 commented on May 4, 2024

https://lucifer.ren/blog/2019/09/22/reverseList/

from leetcode.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.