【链表】有序链表中移除重复项

 1 public class Main {
 2
 3     public Node removeDup(Node node){
 4
 5         if (node == null || node.next == null || node.next.next == null){
 6             return node;
 7         }
 8
 9         Node pre = node.next;
10         Node cur = node.next.next;
11
12         while (cur != null){
13             if (cur.data == pre.data){
14                 pre.next = cur.next;
15             }else {
16                 pre = cur;
17             }
18             cur = cur.next;
19         }
20
21         return node;
22     }
23
24
25     public Node createListNodes() {
26         Node node7 = new Node(7, null);
27         Node node6 = new Node(5, node7);
28         Node node5 = new Node(5, node6);
29         Node node4 = new Node(5, node5);
30         Node node3 = new Node(3, node4);
31         Node node2 = new Node(3, node3);
32         Node node1 = new Node(1, node2);
33         Node head = new Node(0, node1); // head pointer
34
35         return head;
36     }
37
38     public static void main(String[] args) {
39         Main main = new Main();
40         Node node = main.removeDup(main.createListNodes());
41
42         if (node != null) {
43             node = node.next;
44             while (node != null) {
45                 System.out.println(node.data);
46                 node = node.next;
47             }
48         }
49     }
50 }

原文地址:https://www.cnblogs.com/jiangyi-uestc/p/10052440.html

时间: 2025-01-16 02:54:44

【链表】有序链表中移除重复项的相关文章

从一个未排序的链表中移除重复项

问题 从一个未排序的链表中移除重复的项? 附, 如果不允许使用临时的缓存,你如何解决这个问题? 分析 如果可以使用额外的存储空间,我们就开一个数组来保存一个元素的出现情况. 对于这种情况,最好的解决方法当然是使用哈希表,但令人非常不爽的是C++标准里是没有 哈希表的(java里有).网上有人用ext下的hash_map,但毕竟不是C++标准里的, 用起来怪怪的,搞不好换个环境就跑不起来了(像Linux和Windows下使用就不一样). 所以,一般用一个数组模拟一下就好了.但,这里要注意一个问题,

【python cookbook】【数据结构与算法】10.从序列中移除重复项且保持元素间顺序不变

问题:从序列中移除重复的元素,但仍然保持剩下的元素顺序不变 解决方案: 1.如果序列中的值时可哈希(hashable)的,可以通过使用集合和生成器解决. 2.如果序列时不可哈希的,想要去除重复项,需要对上述代码稍作修改: key参数的作用是指定一个函数用来将序列中的元素转化为可哈希的类型,如此可以检测重复项.

LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't

18 如何从一个数组中移除重复的元素

两种方法 (1) 使用LINQ (2) 使用List static void RemoveDups(string[] myStringArray) { // LINQ string[] str = myStringArray.Distinct().ToArray(); // Array to List to Array List<String> myStringList = new List<string>(); foreach (string s in myStringArray

判断两个数组中是否有重复项

两个数组判断是否有重复项 $.each(arr1,function(i,value){ val = value; num1 = i;//用来标记当前arr1数组中的重复项 $.each(arr2,function(i,value){ if(val == value){ alert(value); } }); });

链表——有序链表化为平衡二叉查找树

比较直观的解法是自顶向下的递归解决,先找到中间节点作为根节点,然后递归左右两部分.所有我们需要先找到中间节点,题目中要求如果链表结点数为偶,则中间结点为两个中间结点的后一个.对于单链表来说,必须要遍历一边,可以使用快慢指针加快查找速度. 代码如下: </pre>/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(

Lua表数据中移除某项时一些值的问题

在 k,v pairs(x)中Remove一个数据 table.insert(x,"aaa") table.insert(x,"bbb") table.insert(x,"ccc") for i,v in pairs(x) do if i == 2 then --x[i] = nil table.remove(x,2) end print(x[i]) end 结果aaa ccc 置空一个数据 table.insert(x,"aaa&qu

LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重复数字,并且返回不重复数字的个数 遍历操作: 可以使用新的for循环 for (int n : nums){} 每次进行对比,并且更新第一个遇到不相等的元素的下标为i 对数组进行重新赋值操作 当数组长度大于1时,ans初值为1,当数组长度为0时,返回0 参考代码 : package leetcode_5

移除GridView中的重复项

1. The HTML Markup <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"> <Columns> <asp:BoundFiel