cci-Q2.1 未排序链表去重

原文:

Write code to remove duplicates from an unsorted linked list.

FOLLOW UP

How would you solve this problem if a temporary buffer is not allowed?

译文:

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

进一步地,

如果不允许使用临时的缓存,你如何解决这个问题?

首先定义一个node类

public class LinkedListNode {

    int data;
    LinkedListNode next;

    public LinkedListNode(int data) {
        this.data = data;
        this.next = null;
    }
}


1,允许使用临时的缓存,使用一个哈希表,如果遇到没有遇到过的元素写入哈希表,如果遇到的是重复的,删除。

public static void main(String args[]) {
        LinkedListNode head = null;
        LinkedListNode tmp = null;
        int a[] = {1, 2, 3, 3, 2, 1, 4};
        for (int i = 0; i < a.length; i++) {
            LinkedListNode next = new LinkedListNode(a[i]);
            if (i == 0) {
                head = tmp = next;
                continue;
            }
            tmp.next = next;
            tmp = next;
        }

        // Print the list(before) 
        tmp = head;
        while (tmp != null) {
            System.out.println(tmp.data);
            tmp = tmp.next;
        };

        delDup1(head);

        // Print the list(after) 

        while (head != null) {
            System.out.println(head.data);
            head = head.next;
        };
    }

    public static void delDup1(LinkedListNode head) {
        LinkedListNode n = head;
        Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
        LinkedListNode previous = null;
        while (n != null) {
            if (table.containsKey(new Integer(n.data))) {
                previous.next = n.next;
            } else {
                table.put(new Integer(n.data), new Integer(1));
                previous = n;
            }
            n = n.next;
        }
    }


2,不允许使用临时的缓存


 public static void main(String args[]) {
        LinkedListNode head = null;
        LinkedListNode tmp = null;
        int a[] = {1, 2, 3, 3, 2, 1, 4};
        for (int i = 0; i < a.length; i++) {
            LinkedListNode next = new LinkedListNode(a[i]);
            if (i == 0) {
                head = tmp = next;
                continue;
            }
            tmp.next = next;
            tmp = next;
        }

        // Print the list(before) 
        tmp = head;
        while (tmp != null) {
            System.out.println(tmp.data);
            tmp = tmp.next;
        };

        delDup(head);

        // Print the list(after) 

        while (head != null) {
            System.out.println(head.data);
            head = head.next;
        };
    }

    public static void delDup(LinkedListNode head) {
        if (head == null) {
            return;
        }

        LinkedListNode runner = head;
        LinkedListNode runnernext = head;
        LinkedListNode current = head;

        while (current != null) {
            runner = current;
            runnernext = current.next;
            int currentData = current.data;
            while (runnernext != null) {
                if (runnernext.data == currentData) {
                    runner.next = runnernext.next;
                    runnernext = runner.next;
                } else {
                    runner = runnernext;
                    runnernext = runner.next;
                }
            }
            current = current.next;
        }
    }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码是函数 `bsdtcp_server_send` 的定义,它接受五个参数:`data`(要发送的数据的指针),`num`(要发送的数据的数量),`sdidx`(socket描述符索引),`srv`(指向 `bsdtcp_server_ds` 结构体的指针),以及 `timeout`(超时时间)。函数返回一个 `int32_t` 类型的发送计数。 首先,代码定义了一些变量,包括指向 `client_conn_info` 结构体的指针 `cci`,字符变量 `optval`,以及其他一些用于计数和状态的变量。 接下来,代码进行了一系列的条件判断。首先,检查传入的参数是否有效,包括 `data` 不为空且 `num` 大于零,以及 `srv` 和 `srv->res` 都不为空且具有正确的魔数。如果参数检查失败,代码通过 `goto exit` 跳转到函数结束处。 然后,代码根据给定的 `sdidx` 值来确定要使用的客户端连接信息 `cci`。如果 `sdidx` 在有效范围内且对应的连接被标记为使用且活动状态,则获取 `SO_KEEPALIVE` 套接字选项的值。如果该选项的值为0,则跳转到标签 `ok` 处。 如果没有找到合适的客户端连接信息,则使用默认的活动连接信息 `srv->actcci` 进行相同的操作。 如果仍然没有找到合适的客户端连接信息,则遍历所有的连接信息,直到找到一个 `SO_KEEPALIVE` 选项值为0的连接,并将其设置为活动连接信息。如果找到了合适的连接信息,则跳转到标签 `ok` 处。 在标签 `ok` 处,调用 `cci_send` 函数发送数据,并将返回值赋给 `sendcnt` 变量。 最后,通过 `goto exit` 跳转到函数结束处,在 `exit` 标签处返回 `sendcnt` 变量的值。 需要注意的是,这段代码只是一个函数定义,没有提供关于 `cci_send` 函数的具体实现细节。要完整理解代码的功能和行为,需要查看 `cci_send` 函数的实现代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值