{ "question_id": 83, "question_title": "删除排序链表中的重复元素", "difficulty": "简单", "question_content": "

存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次

返回同样按升序排列的结果链表。

 

示例 1:

\"\"
输入:head = [1,1,2]
输出:
[1,2]

示例 2:

\"\"
输入:head = [1,1,2,3,3]
输出:
[1,2,3]

 

提示:

", "topic_link": "https://bbs.csdn.net/topics/600470129", "cpp": "#include \nusing namespace std;\nstruct ListNode\n{\n\tint val;\n\tListNode *next;\n\tListNode() : val(0), next(nullptr) {}\n\tListNode(int x) : val(x), next(nullptr) {}\n\tListNode(int x, ListNode *next) : val(x), next(next) {}\n};\nclass Solution\n{\npublic:\n\tListNode *deleteDuplicates(ListNode *head)\n\t{\n\t\tif (head == nullptr)\n\t\t{\n\t\t\treturn nullptr;\n\t\t}\n\t\tListNode *prev = head;\n\t\tListNode *p = prev->next;\n\t\twhile (p != nullptr)\n\t\t{\n\t\t\tif (p->val != prev->val)\n\t\t\t{\n\t\t\t\tprev->next = p;\n\t\t\t\tprev = p;\n\t\t\t}\n\t\t\tp = p->next;\n\t\t}\n\t\tprev->next = p;\n\t\treturn head;\n\t}\n};", "java": "public class ListNode {\n\tint val;\n\tListNode next;\n\tListNode(int x) {\n\t\tval = x;\n\t}\n}\nclass Solution {\n\tpublic ListNode deleteDuplicates(ListNode head) {\n\t\tif (head == null || head.next == null) {\n\t\t\treturn head;\n\t\t}\n\t\thead.next = deleteDuplicates(head.next);\n\t\tif (head.val == head.next.val)\n\t\t\thead = head.next;\n\t\treturn head;\n\t}\n}", "js": "", "python": "class ListNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.next = None\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution(object):\n\tdef deleteDuplicates(self, head):\n\t\tif head is None:\n\t\t\treturn None\n\t\tpos = head\n\t\twhile pos is not None and pos.next is not None:\n\t\t\tif pos.val == pos.next.val:\n\t\t\t\tpos.next = pos.next.next\n\t\t\telse:\n\t\t\t\tpos = pos.next\n\t\treturn head\n# %%\nl = LinkList()\nlist1 = [1,1,2]\nl1 = l.initList(list1)\ns = Solution()\nprint(l.convert_list(s.deleteDuplicates(l1)))", "status": 1, "keywords": "链表", "license": { "cpp": "https://github.com/begeekmyfriend/leetcode", "python": "https://github.com/qiyuangong/leetcode", "java": "https://blog.csdn.net/weixin_45176257/article/details/106487210" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/82/82_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/82/82_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/82/82_java.ipynb?type=file" }, "notebook_enable": 1 }