Skip to content

Instantly share code, notes, and snippets.

@gaoyike
Created June 23, 2014 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaoyike/badaff09f773e2a7707e to your computer and use it in GitHub Desktop.
Save gaoyike/badaff09f773e2a7707e to your computer and use it in GitHub Desktop.
2.1
/**
* 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;
}
}
}
@jaly50
Copy link

jaly50 commented Jul 11, 2014

main里的代码好丑啊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment