第56题——删除链表中重复的结点

246 阅读1分钟

题目:

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

思路:

非递归的代码:

  • 1.首先添加一个头节点,以方便碰到第一个,第二个节点就相同的情况
  • 2.设置 first ,last 指针,first指针指向当前确定不重复的那个节点,而last指针相当于工作指针,一直往后面搜索。

Java

package nowcoder;

public class S56_DeleteDuplication {
    public static ListNode deleteDuplication(ListNode pHead) {
        ListNode first = new ListNode(-1);//设置一个trick
        first.next = pHead;
        ListNode p = pHead;
        ListNode last = first;
        while (p != null && p.next != null) {
            if (p.val == p.next.val) {
                int val = p.val;
                p = p.next;
                while (p!= null&&p.val == val)
                    p = p.next;
                last.next = p;
            } else {
                last = p;
                p = p.next;
            }
        }
        return first.next;
    }
    public static void main(String[] args){
        S56_DeleteDuplication s56 = new S56_DeleteDuplication();
        int[] array = {1, 2, 3, 3, 4, 4, 5};
        Array2List array2List = new Array2List();
        ListNode head = array2List.array2List(array);
        head = s56.deleteDuplication(head);
        while (head != null){
            System.out.print(head.val+"->");
            head = head.next;
        }
    }
}