Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

难度:easy

/**
 * 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 || head.next == null) {
            return head;
        }
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null) {
            if(fast.val != slow.val) {
                slow.next = fast;
                slow = slow.next;
            }
            fast = fast.next;
        }
        slow.next = null;
        return head;
    }
}

Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

难度:medium

/**
 * 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 || head.next == null) {
            return head;
        }
        ListNode dummyHead = new ListNode(0);
        ListNode ptr = dummyHead;
        ListNode prePtr = head;
        ListNode nextPtr = head.next;
        while(prePtr != null) {
            if(prePtr.next != null && prePtr.val == prePtr.next.val) {
                nextPtr = prePtr.next;
                while(nextPtr != null && nextPtr.val == prePtr.val) {
                    nextPtr = nextPtr.next;
                }
                prePtr = nextPtr;
            } else {
                ptr.next = prePtr;
                ptr = prePtr;
                prePtr = prePtr.next;
            }
        }
        ptr.next = null;
        return dummyHead.next;
    }
}

Remove Linked List Elements

Easy

Remove all elements from a linked list of integers that have value val.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummyHead = new ListNode(0);
        ListNode ptr = dummyHead;
        while(head != null) {
            if(head.val != val) {
                ptr.next = head;
                ptr = ptr.next;
            }
            head = head.next;
        }
        ptr.next = null;
        return dummyHead.next;
    }
}

Delete Node in a Linked List

Easy

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }
}

Reverse Linked List

Easy

Reverse a singly linked list.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode ptr = null;
        ListNode newHead = ptr;
        while(head != null) {
            newHead = head;
            head = head.next;
            newHead.next = ptr;
            ptr = newHead;
        }
        return newHead;
    }
}

results matching ""

    No results matching ""