83. Remove Duplicates from Sorted List

网友投稿 724 2022-10-09

83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3.

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

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode prev = new ListNode(-1); ListNode curr = head; while(curr != null) { if(curr.val == prev.val) { prev.next = curr.next; } else { prev = curr; } curr = curr.next; } return head; }}

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Mango 一个模块化的web应用程序Go框架(mango的复数)
下一篇:Kyma是一个云原生应用程序开发框架(云原生应用程序架构)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~