-
-
Save gaoyike/badaff09f773e2a7707e to your computer and use it in GitHub Desktop.
2.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Readman on 6/23/14. | |
*/ | |
public class removeDupinLinkedList { | |
public static void removeDupinLinkedList(ListNode head) { | |
if (head == null || head.next == null) | |
return; | |
ListNode slow = head; | |
ListNode fast = head.next; | |
ListNode pre = head; | |
while (slow != null) { | |
while (fast != null) { | |
if (slow.val == fast.val) { | |
pre.next = fast.next; | |
} | |
else { | |
pre = pre.next; | |
} | |
fast = fast.next; | |
} | |
pre = slow.next; | |
slow = slow.next; | |
if (slow != null) | |
fast = slow.next; | |
} | |
} | |
public static void main(String[] args) { | |
ListNode head = new ListNode(1); | |
head.next = new ListNode(3); | |
head.next.next = new ListNode(3); | |
head.next.next.next = new ListNode(2); | |
head.next.next.next.next= new ListNode(2); | |
head.next.next.next.next.next = new ListNode(2); | |
head.next.next.next.next.next.next = new ListNode(2); | |
removeDupinLinkedList(head); | |
while (head != null) { | |
System.out.print(head.val + " "); | |
head = head.next; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
main里的代码好丑啊