链表是线性数据结构,我们给出了一个由整数组成的排序链表。有一些数字可能重复或重复,我们必须将其删除。由于给定的链表已排序,我们可以简单地对其进行迭代,并使用 while 循环可以从中删除重复的节点。我们将通过时间和空间复杂度的讨论来实现适当的代码,以便更好地理解逻辑。
示例
Given linked list is: 1-> 2 -> 2 -> 3 -> 4 -> 4 -> 4 -> 5 -> 5 -> 5-> 6-> null Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
说明 – 给定的链表是排序的,这使得很容易找到重复的元素,如果它们等于以前的值,我们可以通过跳过来删除它们。
让我们看看代码的实现方式
方法
我们将按照以下步骤来解决问题 –
-
首先,我们将创建一个类来为链表的节点提供结构。
-
其次,我们将创建打印链表并向现有链表添加新节点的函数。
-
我们将创建一个函数来传递要从中删除重复元素的链表的头,它将返回新链表的头。
-
首先,我们将检查链表是否为空或者其大小是否等于 1。在这些情况下,我们将按原样返回头部。
-
我们将创建两个变量,一个指示头部,另一个指示头部的下一个节点。
-
如果当前节点和下一个节点的值相等,那么我们会将下一个节点移动到下一个节点,并更新当前节点的下一个节点的地址。
-
否则,我们将移动到下一个节点并将下一个节点移动到其下一个节点。
-
最后我们将返回头部并打印其中存在的值。
示例
让我们在代码中实现给定的步骤以便更好地理解
// class to provide structure to linked list node
class Node{
constructor(val){
this.value = val
this.next = null
}
}
// function to print the linked list
function print(head){
var temp = head;
if(head == null){
console.log("The given linked list is empty");
} else {
var ans = ""
while(temp.next != null){
ans += temp.value;
ans += " -> "
temp = temp.next
}
ans += temp.value
ans += " -> null"
}
console.log(ans)
}
// function to add data in linked list
function add(data, head, tail){
var new_node = new Node(data);
if(head == null){
head = new_node
return new_node
} else {
tail.next = new_node;
return new_node
}
}
// function to remove the duplicate numbers
function removeDupli(head){
// if linked list is empty
if(head == null){
return head;
}
// if linked list is of size one
if(head.next == null){
return head;
}
var temp = head
var next = head.next
while(next != null){
if(temp.value == next.value){
next = next.next;
temp.next = next;
} else {
next = next.next;
temp = temp.next;
}
}
return head;
}
// defining linked list
var head = new Node(1)
var tail = head
tail = add(2,head, tail)
tail = add(2,head, tail)
tail = add(3,head, tail)
tail = add(4,head, tail)
tail = add(4,head, tail)
tail = add(4,head, tail)
tail = add(5,head, tail)
tail = add(5,head, tail)
tail = add(5,head, tail)
tail = add(6,head, tail)
console.log("The given linked list is: ")
print(head)
// calling function to remove duplicate elements
head = removeDupli(head)
console.log("The Linked list after removal of duplicate integers is: ")
print(head)
时间和空间复杂度
上述代码的时间复杂度为 O(N),其中 N 是给定链表中的节点总数。时间复杂度是线性的,因为我们只遍历了链表一次。
上述代码的空间复杂度为 O(1),因为我们没有使用任何额外的空间。
结论
在本教程中,我们实现了一个 JavaScript 程序,用于从给定的排序链表中删除重复元素。由于链表是排序的,因此所有重复元素都彼此相邻,并且可以通过遍历它轻松删除。我们实现的程序时间复杂度为O(N),空间复杂度为O(1)。
以上就是用于从排序链接列表中删除重复项的 Javascript 程序的详细内容,更多请关注双恒网络其它相关文章!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » 用于从排序链接列表中删除重复项的 Javascript 程序
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?