算法训练营Day3|链表part01

文章讲述了如何在Java中移除链表中的特定元素,使用虚拟头节点简化操作;介绍了MyLinkedList类的实现,包括get、add、delete和addAtIndex方法,以及链表反转的迭代和递归实现。
摘要由CSDN通过智能技术生成

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 dummyHead = new ListNode(0,head);
              ListNode pre = dummyHead;
              ListNode cur = dummyHead.next;
              while(cur!=null){
                  if(cur.val==val){
                      // 移除的元素pre不变,cur变
                      pre.next = cur.next;
                  }else{
                      // 未移除的话都变
                      pre = pre.next;
                  }
                  cur = cur.next;
              }
              return dummyHead.next;
          }
      }
    • 收获
      • 方法:添加虚拟头节点。俩指针来操控当前node与之前node。要移除的就pre指向cur.next;这时只移动cur,pre不变
  • 题解心得
    • 使用方法一样,皆为头节点。但其添加了一个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) {
              if(head==null){
                  return head;
              }
              ListNode dummyHead = new ListNode(0,head);
              ListNode pre = dummyHead;
              ListNode cur = dummyHead.next;
              while(cur!=null){
                  if(cur.val==val){
                      // 移除的元素pre不变,cur变
                      pre.next = cur.next;
                  }else{
                      // 未移除的话都变
                      pre = pre.next;
                  }
                  cur = cur.next;
              }
              return dummyHead.next;
          }
      }

707.设计链表

  • 自我尝试
    • 代码
      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() {
              this.head = new ListNode(-1);
              this.size = 0;
          }
          
          public int get(int index) {
              if(index>=size){
                  return -1;
              }
              ListNode cur = head;
              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) {
              ListNode cur = head;
              while(cur.next!=null){
                  cur = cur.next;
              }
              cur.next = new ListNode(val);
              this.size++;
          }
          
          public void addAtIndex(int index, int val) {
              if(index==this.size){
                  addAtTail(val);
              }else if(index>this.size){
                  //Invalid operation
                  return;
              }else{
                  //插入
                  ListNode pre=this.head;
                  ListNode cur=this.head.next;
                  for(int i=0;i<index;i++){
                      pre=cur;
                      cur=cur.next;
                  }
                  pre.next = new ListNode(val);
                  pre.next.next = cur;
                  this.size++;
              }
          }
          
          public void deleteAtIndex(int index) {
              if(index>=this.size){
                  // Invalid operation
                  return;
              }
              ListNode pre=this.head;
              for(int i=0;i<index;i++){
                  pre=pre.next;
              }
              pre.next=pre.next.next;
              this.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);
       */

    • 收获
      • 使用dummyhead提供遍历的便捷性
      • 引入size属性,在方法的实现中提供便利
  • 题解心得
    • 代码
      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() {
              this.head = new ListNode(-1);
              this.size = 0;
          }
          
          public int get(int index) {
              // 小于0的index也考虑;
              if(index<0 ||index>=size){
                  return -1;
              }
              ListNode cur = head;
              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) {
              //直接也使用addAtIndedx
              addAtIndex(this.size,val);
          }
          
          public void addAtIndex(int index, int val) {
               if(index>this.size){
                  //Invalid operation
                  return;
              }else{
                  //可以只使用单个指针
                  ListNode pre=this.head;
                  for(int i=0;i<index;i++){
                      pre=pre.next;
                  }
                  pre.next = new ListNode(val,pre.next);
                  this.size++;
              }
          }
          
          public void deleteAtIndex(int index) {
              if(index>=this.size){
                  // Invalid operation
                  return;
              }
              ListNode pre=this.head;
              for(int i=0;i<index;i++){
                  pre=pre.next;
              }
              pre.next=pre.next.next;
              this.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);
       */

    • 收获
      • addAtIndex方法里可以也通过一个指针直接进行操作。没必要两个。

206.反转链表

  • 自我尝试
    • 代码使用迭代法,通过三个pointer操作
      /**
       * 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 tmp;
              ListNode cur=head;
              ListNode pre=null;
              while(cur!=null){
                  tmp = cur.next;
                  cur.next = pre;
                  pre=cur;
                  cur = tmp;
              }
              return pre;
      
          }
      }

  • 题解心得
    • 递归法比较难。递进到最底层,确定了新head后,层层传出来!同时在这个过程中进行链接的reverse
      /**
       * 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) {
              // 边缘条件,传入为null
              if(head==null){
                  return null;
              }
              // 边缘条件,确定最后一个node,并且返回
              if (head.next==null) return head;
              // 将head层层传递出来通过last
              ListNode last = reverseList(head.next);
              //反转
              head.next.next = head;
              //head指为空
              head.next = null;
              System.out.println(last.val);
              return last;
          }
      }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值