洞察探索open banking如何通过小程序容器技术助力金融企业实现数据安全和数字化转型
724
2022-10-09
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小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~