Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given1->1->2
, return 1->2
. Given 1->1->2->3->3
, return 1->2->3
. 就是简单地去除链表中重复元素。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head == null) return null; ListNode temp = head; ListNode jump = temp.next; Listlist = new ArrayList (); list.add(head.val); while(jump!=null){ int nextVal = jump.val; if(list.contains(nextVal)){ jump = jump.next; }else{ list.add(nextVal); temp = jump; jump = temp.next; } temp.next = jump; } return head; }}
我的思路是用一个ArrayList存储重复数据,然后用temp和jump存储断开的点和每次移动的点。