yan chuanli пре 2 година
родитељ
комит
98bfb8dc45
1 измењених фајлова са 29 додато и 6 уклоњено
  1. 29 6
      linklist/delete_duplicates.py

+ 29 - 6
linklist/delete_duplicates.py

@@ -1,20 +1,43 @@
 from typing import Optional
-from utils import ListNode, list_to_linklist
+from utils import ListNode, list_to_linklist, deleteNode, linkList_to_arr
 
 def deleteDuplicates(head: Optional[ListNode]) -> Optional[ListNode]:
     if not head:
         return head
+    # p = head
+    # while p.next:
+    #     if p.val == p.next.val:
+    #         p.next = p.next.next
+    #     else:
+    #         p = p.next
+    #
+    # return head
+    # p = head
+    # while p and p.next:
+    #     q: ListNode = p.next
+    #     # 寻找跟p不同val的节点
+    #     # 找到的时候q指向了它
+    #     while q and q.val == p.val:
+    #         q = q.next
+    #     p.next = q
+    #     p = q
+    # return head
 
     p = head
-    while p.next:
-        if p.val == p.next.val:
-            p.next = p.next.next
+    q = head.next
+    while q:
+        if q.val == p.val:
+            p.next = q.next
+            q = q.next
         else:
-            p = p.next
+            p = q
+            q = q.next
 
     return head
 
-print(deleteDuplicates(list_to_linklist([1, 1, 2])))
+res = deleteDuplicates(list_to_linklist([1]))
+print(linkList_to_arr(res))
+