Remove Duplicates from Sorted List

Source: Internet
Author: User

https://leetcode.com/problems/remove-duplicates-from-sorted-list/

Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such this each element appear only once.

For example,
Given 1->1->2 , return 1->2 .
Given 1->1->2->3->3 , return 1->2->3 .

Title: Removing duplicates of an ordered list

1 #Definition for singly-linked list.2 #class ListNode:3 #def __init__ (self, x):4 #self.val = x5 #Self.next = None6 7 classSolution:8     #@param {ListNode} head9     #@return {ListNode}Ten     defdeleteduplicates (Self, head): One         ifHead==noneorhead.next==None: A             returnHead -p=Head -          whileP.next:#the previous if judgment is guaranteed to p!=none at this time, only to judge P.next the             ifP.val==p.next.val:#if the P-value is equal to the P.next value at this time -P.next=p.next.next#Remove the P.next and connect directly to the p.next.next without changing p, while still comparing p and p.next at this time -             Else:                         -P=p.next#if the p and P.next values are not equal, move the position of P and start judging the next node and the values followed.  +         returnHead

Remove Duplicates from Sorted List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.