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))
+