Java实现-删除排序链表的重复元素1

xiaoxiao2021-02-28  124

/** * Definition for ListNode * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { /** * @param ListNode head is the head of the linked list * @return: ListNode head of linked list */ public static ListNode deleteDuplicates(ListNode head) { // write your code here if(head==null){ return null; } ListNode list=head; while(list.next!=null){ if(list.val==list.next.val){ if(list.next.next==null){ list.next=null; }else{ ListNode node=list.next; list.next=node.next; } }else{ list=list.next; } } return head; } }

转载请注明原文地址: https://www.6miu.com/read-37327.html

最新回复(0)