代码随想录算法训练营第三天|链表|Leetcode 203.移除链表元素 ● 707.设计链表 ● 206.反转链表

203.移除链表元素 Remove Linked List Elements - LeetCode

先判断是不是头节点

加入虚拟头节点

while (head != null && head.val == val)

        head = head.next;

        cue = head;

while (cur != null && cur.next != null)

        if (cur.next == val)

                cur.next = cur.next.next;

        else

                cur = cur.next;

return head;

使用dummyHead

dummyHead = new ();

dummyHead.next = head;

cur = dummyHead;//为什么要等于dummyHead,不等于dummyHead.next

while (cur.next != null)

        if (cur.next == val)

                cur.next = cur.next.next;

        else

                cur = cur.next;

return dummy.next;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) return head;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode cur = dummy;

        while (cur.next != null) {
            if (cur.next.val == val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        return dummy.next;
    }
}

707.设计链表 

1.获取第几个节点的值

cur = dummy.next;

while (n){

        cur = cur.next;

        n--;

}

return cur.val;

2.头部插入节点,newNode要先指向头节点

newNode = new ();

newNode.next = dummy.next;

dummy.next = newNode;

size++;

3.尾部插入节点

dummy = new Node();

cur = dummy;

while(cur.next != null) {

        cur = cur.next;

}

cur.next = newNode;

4.第n个节点前,插入节点

dummy = new Node();

cur = dummy;

while (n--){

        cur = cur.next;

        n--;

}

newNode.next = cur.next;

cur.next = newNode;

删第N个节点

while(n){

cur = cur.next;

n--;

}

cur.next = cur.next.next;

size--;

不懂为啥我的code AC不了。。。

// // class ListNode{
// //     int val;
// //     ListNode next;
// //     ListNode() {}
// //     ListNode(int val){
// //         this.val = val;
// //     }
// // }
// class MyLinkedList {
//     int size;
//     ListNode head;

//     public MyLinkedList() {
//         size = 0;
//         head = new ListNode(0);
//     }
    
//     public int get(int index) {
//         // if (index < 0 || index >= size) return -1;

//         // ListNode cur = head;
//         // while (index-- >= 0) {
//         //     cur = cur.next;
//         // }
//         // return cur.val;
//         if (index < 0 || index >= size) {
//             return -1;
//         }
//         var cur = head.next;
//         while (index-- > 0) {
//             cur = cur.next;
//         }
//         return cur.val;

//     }
    
//     public void addAtHead(int val) {
//         // ListNode newNode = new ListNode(val);
//         // newNode.next = head.next;
//         // head.next = newNode;
//         // size++;
//         addAtIndex(0, val);
//     }
    
//     public void addAtTail(int val) {
//         // ListNode newNode = new ListNode(val);
//         // ListNode cur = head;
//         // while (cur.next != null) {
//         //     cur = cur.next;
//         // }
//         // cur.next = newNode;
//         // size++;
//         addAtIndex(size, val);
//     }
    
//     public void addAtIndex(int index, int val) {
//         if (index > size) return;
//         if (index < 0) {
//             index = 0;
//         }
//         ListNode newNode = new ListNode(val);
//         ListNode cur = head;
//         while (index-- > 0) {
//             cur = cur.next;
//         }
//         newNode.next = cur.next;
//         cur.next = newNode;
//         size++;
//     }
    
//     public void deleteAtIndex(int index) {
//         if (index >= size) return;
//         if (index == 0) {
//             head = head.next;
//             return;
//         }
//         ListNode cur = head;
//         while (index-- > 0) {
//             cur = cur.next;
//         }
//         cur.next = cur.next.next;
//         size--;
//     }
// }
class MyLinkedList {
    private ListNode dummy = new ListNode();
    private int cnt;

    public MyLinkedList() {

    }

    public int get(int index) {
        if (index < 0 || index >= cnt) {
            return -1;
        }
        var cur = dummy.next;
        while (index-- > 0) {
            cur = cur.next;
        }
        return cur.val;
    }

    public void addAtHead(int val) {
        addAtIndex(0, val);
    }

    public void addAtTail(int val) {
        addAtIndex(cnt, val);
    }

    public void addAtIndex(int index, int val) {
        if (index > cnt) {
            return;
        }
        var pre = dummy;
        while (index-- > 0) {
            pre = pre.next;
        }
        pre.next = new ListNode(val, pre.next);
        cnt++;
    }

    public void deleteAtIndex(int index) {
        if (index < 0 || index >= cnt) {
            return;
        }
        var pre = dummy;
        while (index-- > 0) {
            pre = pre.next;
        }
        var t = pre.next;
        pre.next = t.next;
        t.next = null;
        cnt--;
    }
}




/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */

206.反转链表 Reverse Linked List - LeetCode

recursive:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
}

Iterator:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null) return head;
        ListNode pre = null;
        ListNode next = null;
        ListNode cur = head;

        while (cur != null) {
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }

        return pre;
    }
}

你可能感兴趣的:(代码随想录算法训练营,链表,算法,数据结构)