# 29. 删除排序链表中的重复元素

java

```java
// 链表工具类
public class LinkedUtils {
    public static ListNode arrayToLinkedList(int[] array){
        if(array==null) return null;
        //head node 
        ListNode root=new ListNode(-1);
        ListNode p=root;

        int size=array.length;
        int i=0;
        while(i<size){
            ListNode q=new ListNode(array[i++]);
            q.next=null;
            p.next=q;
            p=q;
        }

        return root.next;
    }

    public static void print(ListNode root) {
        if(root==null) System.out.println("root is null");
        else{
            while(root!=null){
                System.out.print(root.val);
                if(root.next!=null){
                    System.out.print("->");
                }
                root=root.next;
            }
            System.out.println();
        }
    }

    public static int length(ListNode root) {
        if(root==null) return 0;
        int n=0;
        while(root!=null){
            root=root.next;
            n++;
        }
        return n;
    }

    // 返回指向链表尾部的指针
    public static ListNode moveToTail(ListNode root) {
        if(root==null) return null;
        ListNode tail=root;
        while(tail.next!=null){
            tail=tail.next;
        }
        return tail;
    }
}


// 定义链表
public class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int val) {
        this.val=val;
    }
}


/**
 * 83. 删除排序链表中的重复元素.
 * 给定一个排序链表，删除所有重复的元素，使得每个元素只出现一次。
 *
 * 示例 1:
 * 输入: 1->1->2
 * 输出: 1->2
 *
 * 示例 2:
 * 输入: 1->1->2->3->3
 * 输出: 1->2->3
 */
public class Solution83 {
    /**
     * 从第二个元素开始，和它之前的元素比较，
     * 如果相同，删除当前节点，继续比较直至末尾
     * @param head
     * @return
     */
    public ListNode deleteDuplicates(ListNode head) {
        ListNode p=head,q=null;
        while(p!=null){
            if(q!=null&&p.val==q.val) {
                q.next=p.next;
                p=q.next;
            }else{
                q=p;
                p=p.next;
            }
        }

        return head;
    }

    public static void main(String[] args) {
        int[] array={
                1,1,2,2,2,2,2
        };
        ListNode head=LinkedUtils.arrayToLinkedList(array);
        ListNode r=new Solution83().deleteDuplicates(head);
        LinkedUtils.print(r);
    }
}
```

python

```
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/shu-ju-jie-gou-lei/29-shan-chu-pai-xu-lian-biao-chong-fu-yuan-su.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
