leetcode 83. 删除排序链表中的重复元素

【前言】

           python刷leetcode题解答目录索引:https://blog.csdn.net/weixin_40449300/article/details/89470836

           github链接:https://github.com/Teingi/test 

【正文】

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:

输入: 1->1->2
输出: 1->2

示例 2:

输入: 1->1->2->3->3
输出: 1->2->3

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
 
class Solution:
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        #此为不带头结点的链表
        if head is None:#链表为空
            return head
        cur=head
        while cur.next:#下一节点不为空
            if cur.val==cur.next.val:#第一次判断,头元素与头元素下一节点的值是否相等。。。
                cur.next=cur.next.next
            else:
                cur=cur.next
        return head

java代码:

/**
 * 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;
        if(cur == null){
            return head;
        }
        while(cur.next != null){
            if(cur.val == cur.next.val){
                cur.next = cur.next.next;
            }
            else{
                cur = cur.next;
            }
        }
        return head;
    }
}

 

你可能感兴趣的:(Leetcode,Java,Test)