==========================努力奋斗财源广进==========================
删除给出链表中的重复元素(链表中元素从小到大有序),使链表中的所有元素都只出现一次。

import java.util.*;
/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */
public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        //题目简单死了。
        if(head == null || head.next == null) 
        {
            return head;
        }
        ListNode cur = head;
        while(cur != null && cur.next != null){
            int val = cur.val;
            if(val == cur.next.val){
                cur.next = cur.next.next;
            } else{
                cur = cur.next;
            }
        }
        return head;
    }
}

//待定
此题比较简单,只要理清脉络,就能很快的解体出来。
评论区记录复习记录
评论