leetcode:Remove Duplicates from Sorted List

来源:互联网 发布:ubuntu更新软件的方法 编辑:程序博客网 时间:2024/05/16 10:24


去除链表中重复的元素并只保留一个

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode deleteDuplicates(ListNode head) {        if(head != null && head.next != null){        ListNode pre = head, next = head.next;        while(next != null){        while(next != null && pre.val == next.val){        next = next.next;        }        pre.next = next;        pre = next;        if(pre != null)            next = pre.next;        }        }        return head;    }}


0 0
原创粉丝点击