• remove-duplicates-from-sorted-list


    /**
    * Given a sorted linked list, delete all duplicates such that each element appear only once.
    * For example,
    * Given1->1->2, return1->2.
    * Given1->1->2->3->3, return1->2->3.
    *
    * 给定一个已排序的链接列表,删除所有重复项,使每个元素只出现一次。
    * 例如,
    * 给出1->1->2,返回1->2。
    * 给出1->1->2->3->3,返回1->2->3。
    */

    /**
     * Given a sorted linked list, delete all duplicates such that each element appear only once.
     * For example,
     * Given1->1->2, return1->2.
     * Given1->1->2->3->3, return1->2->3.
     *
     * 给定一个已排序的链接列表,删除所有重复项,使每个元素只出现一次。
     * 例如,
     * 给出1->1->2,返回1->2。
     * 给出1->1->2->3->3,返回1->2->3。
     */
    
    public class Main38 {
        public static void main(String[] args) {
            ListNode head = new ListNode(1);
            head.next = new ListNode(1);
    //        head.next.next = new ListNode(2);
    //        head.next.next.next = new ListNode(3);
            System.out.println(Main38.deleteDuplicates(head).val);
        }
    
        public static class ListNode {
            int val;
            ListNode next;
            ListNode(int x) {
                val = x;
                next = null;
            }
        }
    
        public static ListNode deleteDuplicates(ListNode head) {
    
            ListNode ln = head;
            while (ln != null) {
                while (ln.next != null && ln.val == ln.next.val) {
                    ln.next = ln.next.next;
                }
                ln = ln.next;
            }
            return head;
        }
    }
    

      

  • 相关阅读:
    Linux守护进程
    sequel pro无法连接mysql服务器
    socket编程之并发回射服务器2
    Unix的I/O模型
    nginx.conf laravel 配置
    phpstudy使用PHP+nginx配置Laravel
    nginx配置文件分开配置
    centos安装composer
    linux下 设置php的环境变量 php: command not found
    laravel 安装
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11320272.html
Copyright © 2020-2023  润新知