LeetCode 83. Remove Duplicates from Sorted List Add to List

来源:互联网 发布:mac怎么关掉程序 编辑:程序博客网 时间:2024/05/22 16:50
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. 
public class Solution {    public ListNode deleteDuplicates(ListNode head) {        if(head!=null){            ListNode p=new ListNode(0);            ListNode q=new ListNode(0);            p=head;            q=head.next;            int k=0;            while(q!=null){                if(p.val==q.val){                    q=q.next;                    continue;                }                else{                    p.next=q;                    p=q;                    q=q.next;                }            }            p.next=null;                   }        return head;    }}

0 0
原创粉丝点击