代码随想录算法训练营DAY3|203、707、206

链表基础

  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 ListNode{
//双链表节点的定义
    int val;
    ListNode pred;
    ListNode next;
    ListNode(){
    }
    ListNode(int val){
        this.val=val;
    }
}
//链表的遍历。借助指针来完成
ListNode cur=head;
while(cur!=null){
 cur=cur.next;
}

 对链表基础很不熟悉,要多练习。链表的增删改查,修改元素的先后循序。

技巧:链表可用指针遍历。设置虚拟节点来减少增加或删除的复杂度。

203移除链表元素

题目链接:203. 移除链表元素 - 力扣(LeetCode)

 难点:虚拟头节点的使用。

/**
 * 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 dummyhead=new ListNode();
        dummyhead.next=head;
        ListNode cur=dummyhead;
        while(cur!=null){
            if(cur.next!=null&&cur.next.val==val){
                cur.next=cur.next.next;
            }else{
                cur=cur.next;
            }
        }
        // 返回dummyhead的下一个节点,因为头节点可能已经被删掉了
        return dummyhead.next;
    }
}

707设计链表

题目链接:707. 设计链表 - 力扣(LeetCode)

难点:虚拟头节点

//单链表
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 {
    // 虚拟头节点
    ListNode head;
    int size;
    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;
    }
    
    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) return;
        if(index<0)index=0;
        // 链表长度加1
        size++;
        // 找到插入点的前驱
        ListNode pred=head;
        ListNode toadd=new ListNode(val);
        while(index-->0){
            pred=pred.next;
        }
        toadd.next=pred.next;
        pred.next=toadd;
    }
    
    public void deleteAtIndex(int index) {
        if(index<0||index>=size) return;
        size--;
        if(index==0){
            head=head.next;
            return;
        }
        ListNode pred=head;
        for(int i=0;i<index;i++){
            pred=pred.next;
        }
        pred.next=pred.next.next;
    }
    
}
//双链表
class ListNode{
    int val;
    ListNode pred;
    ListNode next;
    ListNode(){
    }
    ListNode(int val){
        this.val=val;
    }
}
class MyLinkedList {
    int size;
    ListNode head; 
    ListNode tail;   
    public MyLinkedList() {
        this.size=0;
        this.head=new ListNode(0);
        this.tail=new ListNode(0);
        head.next=tail;
        tail.pred=head;
    }
    public int get(int index) {
        if(index<0||index>=size)return -1;
        ListNode cur=this.head;
        if(index>=size/2){
            cur=tail;
            for(int i=0;i<size-index;i++){
                cur=cur.pred;
            }
        }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)return;
        if(index<0)index=0;
        size++;
        ListNode pre=head;
        ListNode toadd=new ListNode(val);
        for(int i=0;i<index;i++){
            pre=pre.next;
        }
        toadd.next=pre.next;
        pre.next.pred=toadd;
        toadd.pred=pre;   
        pre.next=toadd;
    }
    
    public void deleteAtIndex(int index) {
        if(index<0||index>=size)return;
        size--;
        ListNode pre=head;
        for(int i=0;i<index;i++){
            pre=pre.next;
        }
        pre.next.next.pred=pre;
        pre.next=pre.next.next;
    }
}

 206反转链表 

题目链接:206. 反转链表 - 力扣(LeetCode)

/**
 * 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 cur=head;
        ListNode pre=null;
        while(cur!=null){
           ListNode temp=cur.next;
           cur.next=pre; 
           pre=cur;
           cur=temp;
        }
        return pre;
    }
}
/**
 * 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(head,null);
    }
    public ListNode reverse(ListNode cur,ListNode pre){
        if(cur==null)return pre;
        ListNode temp=cur.next;
        cur.next=pre;        
        return reverse(temp,cur);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值