代码随想录算法训练营第二天|LeetCode203移除链表元素、LeetCode707设计链表、LeetCode206反转链表

Day3

LeetCode203 移除链表元素

题目链接:移除链表元素

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

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

         ListNode curr = dummy;
         while(curr.next != null){
            if(curr.next.val == val) curr.next = curr.next.next;
            else curr = curr.next;
         }

         return dummy.next;
    }
}

LeetCode707 设计链表

题目链接:设计链表

/*
单链表
*/
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 MyLinkedList {
 
    int size;

    ListNode head;

    public MyLinkedList() {
        size = 0;
        head = new ListNode();
    }

    public int get(int index) {
        if (index < 0 || index >= size) {
            return -1;
        }
        ListNode curr = head;
        while(index-- >= 0) curr= curr.next;
        return curr.val;
    }

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

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


    public void addAtIndex(int index, int val) {
        if (index > size || index < 0) {
            return;
        }
        size++;
        ListNode prev = head;
        while(index-- > 0) prev = prev.next;
        prev.next = new ListNode(val, prev.next);
    }

    public void deleteAtIndex(int index) {
        if (index < 0 || index >= size) {
            return;
        }
        size--;
        ListNode prev = head;
        while(index-- > 0) prev = prev.next;
        prev.next = prev.next.next;
    }
}
/*
双链表
*/
class ListNode{
    int val;
    ListNode next,prev;
    ListNode() {};
    ListNode(int val){
        this.val = val;
    }
}


class MyLinkedList {  


    int size;
    ListNode head,tail;
    
    public MyLinkedList() {
        this.size = 0;
        this.head = new ListNode(0);
        this.tail = new ListNode(0);
        head.next=tail;
        tail.prev=head;
    }
    
    public int get(int index) {
        if(index>=size || index < 0)  return -1;
        
        ListNode cur = this.head;
        
        if(index >= size / 2){
            cur = tail;
            for(int i=0; i< size-index; i++){
                cur = cur.prev;
            }
        }else{
            for(int i=0; i<= index; i++){
                cur = cur.next; 
            }
        }
        return cur.val;
    }
    
    public void addAtHead(int val) {
        addAtIndex(0,val);
    }
    
    public void addAtTail(int val) {
        addAtIndex(size,val);
    }
    
    public void addAtIndex(int index, int val) {
        if(index>size || index < 0) return;
        
        size++;
        ListNode prev = this.head;
        for(int i=0; i<index; i++){
            prev = prev.next;
        }
        ListNode newNode = new ListNode(val);
        newNode.next = prev.next;
        prev.next.prev = newNode;
        newNode.prev = prev;
        prev.next = newNode;
        
    }
    
    public void deleteAtIndex(int index) {
        if(index>=size || index < 0) return;
        
        size--;
        ListNode prev = this.head;
        for(int i=0; i<index; i++){
            prev = prev.next;
        }
        prev.next.next.prev = prev;
        prev.next = prev.next.next;
    }
}

LeetCode206 反转链表

题目链接:反转链表

/**
 * 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 null;
        ListNode prev = null;
        ListNode curr = head;

        while(curr != null){
            ListNode nextNode = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextNode;
        }
        return prev;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值