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

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

203.移除链表元素

/**
 * 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 dummyNode = new ListNode();
        dummyNode.next = head;

        ListNode currentNode = dummyNode;
        while(currentNode.next != null)
        {
            if(currentNode.next.val == val)
            {
                currentNode.next = currentNode.next.next;
            }
            else
            {
                currentNode = currentNode.next;
            }
        }
        
        return dummyNode.next;
    }
}

707. 设计链表

  • 较难,需要多次复习
class MyLinkedList {

    class ListNode
    {
        int val;
        ListNode next;
        ListNode(int val)
        {
            this.val = val;
        }
    }

    //储存链表元素个数
    private int size;
    //定义虚拟头节点,并非真正的头节点
    private ListNode head;

    //链表初始化
    public MyLinkedList() {
        this.size = 0;
        this.head = new ListNode(0);
    }
    
    //获得链表第index个节点的值
    public int get(int index) {
        if(index < 0 || index >= size)
        {
            return -1;   
        }

        ListNode curr = head;
        for(int i = 0; i <= index; i++)
        {
            curr = curr.next;
        }

        return curr.val;
    }
    
    public void addAtHead(int val) {
        ListNode dummy = new ListNode(val);
        dummy.next = head.next;
        head.next = dummy;
        size++;        
    }
    
    public void addAtTail(int val) {
        ListNode curr = head;
        ListNode dummy = new ListNode(val);
        while(curr.next != null)
        {
            curr = curr.next;
        }
        curr.next = dummy;
        size++;

    }
    
    public void addAtIndex(int index, int val) {
        if(index < 0 || index > size)
        {
            return;
        }

        ListNode curr = head;
        ListNode dummy = new ListNode(val);
        for(int i = 0; i < index; i++)
        {
            curr = curr.next;
        }

        dummy.next = curr.next;
        curr.next = dummy;
        size++;        
    }
    
    public void deleteAtIndex(int index) {
        if(index < 0 || index >= size)
        {
            return ;
        }

        ListNode curr = head;
        for(int i = 0; i < index; i++)
        {
            curr = curr.next;
        }
        curr.next = curr.next.next;
        size--;
    }
}

/**
 * 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. 反转链表

  • 递归写法
/**
 * 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) {
        return reverse(null, head);
    }

    public ListNode reverse(ListNode pre, ListNode curr)
    {
        if(curr == null)
        {
            return pre;
        }

        ListNode temp = null;
        temp = curr.next;
        curr.next = pre;
        return reverse(curr, temp);
        
    }
}
  • 双指针写法
/**
 * 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) {
        ListNode curr = head;
        ListNode pre = null;
        ListNode temp = null;
        while(curr != null)
        {
            temp = curr.next;
            curr.next = pre;
            pre = curr;
            curr = temp;
        }
        return pre;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值