83. Remove Duplicates from Sorted List

MA:
/**
 * 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 temp;
        ListNode cur=head;
        while(cur!=null){
                temp=cur.next;
                while(temp!=null && temp.val==cur.val){
                    temp=temp.next;
                }
                cur.next=temp;
                cur=temp;
        }
        return head;
    }
}

OA:
public ListNode deleteDuplicates(ListNode head) {
    ListNode current = head;
    while (current != null && current.next != null) {
        if (current.next.val == current.val) {
            current.next = current.next.next;
        } else {
            current = current.next;
        }
    }
    return head;
}
CA:
/**
 * 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 cur=head;
        while(cur!=null&&cur.next!=null){
            if(cur.next.val==cur.val){
                cur.next=cur.next.next;
            }else{
                cur=cur.next;
            }
        }
        return head;
    }
}

评论

此博客中的热门博文

225 Implement Stack using Queues

232. Implement Queue using Stacks

20. Valid Parentheses