Skip to content

Instantly share code, notes, and snippets.

@cxtadment
Created September 2, 2015 17:16
Show Gist options
  • Select an option

  • Save cxtadment/6e9eea7842ee3130dc76 to your computer and use it in GitHub Desktop.

Select an option

Save cxtadment/6e9eea7842ee3130dc76 to your computer and use it in GitHub Desktop.
/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
if (head == null || head.next == null) {
return head;
}
ListNode pre = head;
ListNode curr = head.next;
while (curr != null) {
if (pre.val == curr.val) {
curr = curr.next;
} else {
pre.next = curr;
pre = pre.next;
}
}
pre.next = null;
return head;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment