1. 程式人生 > >單鏈表練習1--去重

單鏈表練習1--去重

在進行單鏈表去重之前,我們先來考慮一下,陣列如何就地去重。

我們可以很容易的編寫出這樣的程式碼:

如果通過正向遍歷來刪除的話,就要注意控制遍歷的長度。

function arrRemoveRepeatFront(arr) {
	for(var i=0; i<arr.length; ++i) {
		if(arr.indexOf(arr[i]) < i) {
			arr.splice(i, 1);
			i--;      // 保留前面遇到的元素,刪除後面遇到的重複元素
		}
	}

	return arr;
}

console.log(arrRemoveRepeatFront([1, 2, 2, 2, 3, 3, 4, 3]))
console.log(arrRemoveRepeatFront([1]))

如果我們從陣列尾部開始遍歷,不用擔心陣列長度的改變。比較好理解
function arrRemoveRepeatBack(arr) {
	var index = arr.length - 1;
	while(index >= 0) {
		if(arr.indexOf(arr[index]) < index) {
			arr.splice(index, 1);
		}

		index--;     // 注意看index 是在if裡面還是外面。
	}

	return arr;
}

好,我們借鑑上面的思路,
function LinkedList() {
	var Node = function (element) {
		this.element = element;
		this.next = null;
	}

	var head = null;   // keep the head pointer
	var length = 0;    // keep the length of linked list

	this.append = function (element) {
		var new_node = new Node(element);
		if(head == null) {
			head = new_node;
		}else {
			var current = head;
			while(current.next != null) {
				current = current.next;
			}

			current.next = new_node;
		}

		length++;
	}

	this.insert = function (position, element) {
		var new_node = new Node(element);
		if(position <= 0) {
			new_node.next = head;
			head = new_node;
			length++;
			return;
		}

		if(position > this.size() -1) {
			this.append(element);
			return;
		}

		var current = head;
		for(var i=1; i<position; ++i) {
			current = current.next;
		}

		new_node.next = current.next;
		current.next = new_node;
		length++;
	}

	this.remove = function (element) {
		var index = this.indexOf(element);
		if(index == -1) {
			return;
		}

		this.removeAt(index);
	}

	this.removeAt = function (position) {
		if(position < 0 || position > this.size() - 1) {
			throw new Error("Array index exceeds");
		}

		if(position == 0) {
			head = head.next;
		} else {
			var current = head;
			for(var i=1; i<position; ++i) {
				current = current.next;
			}
			current.next = current.next.next;
		}

		length--;
	}

	this.indexOf = function (element) {
		var current = head;
		var cnt = -1;
		while(current != null) {
			cnt++;
			if(current.element == element) {
				return cnt;
			}

			current = current.next;
		}

		return - 1;
	}

	this.size = function () {
		return length;
	}

	this.getHead = function () {
		return head;
	}

	this.print = function () {
		var current = head;
		var arr = []
		while(current != null) {
			arr.push(current.element);
			current = current.next;
		}

		console.log(arr.join(", "));
	}


}


/*
	刪除重複的資料節點
*/

function removeRepeat(linked_list) {
	var current = linked_list.getHead();
	var index;
	var cnt = -1;
	while(current != null) {
		cnt++;
		index = linked_list.indexOf(current.element);
		if(index < cnt) {
			linked_list.removeAt(index);
			cnt--;
		}

		current = current.next;
	}

	return linked_list;
}

/*測試函式*/
function test(arr) {
	var link_list = new LinkedList();
	for(var i=0; i<arr.length; ++i) {
		link_list.append(arr[i]);
	}

	return link_list;
}

var arr = [10, 20, 20, 20, 30, 30, 40, 40, 50, 40];
var result = test(arr);
result.print();
var result_no_repeat = removeRepeat(result);
result_no_repeat.print();