代码随想录算法训练营第三天| 203. 移除链表元素、707. 设计链表、59. 反转链表(Java)
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) {
if(head==null){
return head;
}
//虚拟头节点
ListNode dummy=new ListNode(-1,head);
//遍历起始位置
ListNode pre=dummy;
//定义一个游标
ListNode cur =head;
while(cur!=null){
if(cur.val==val){
pre.next=cur.next;
}else{
pre=cur;
}
cur=cur.next;
}
return dummy.next;
}
}
707 设计链表
package com.shuzijun.leetcode.editor.en;
public class DesignLinkedList{
public static void main(String[] args) {
Solution solution = new DesignLinkedList().new Solution();
}
//leetcode submit region begin(Prohibit modification and deletion)
//class ListNode {
// int val;
// ListNode next;
// ListNode(){}
// ListNode(int val) {
// this.val=val;
// }
// }
class MyLinkedList {
int size;
ListNode head;
//size是实际链表容量长度,index是包含0的索引,index值从0开始,
//比较index和size时,index的0实际是链表头结点的next元素
public MyLinkedList() {
size=0;
head =new ListNode(0);
}
public int get(int index) {
if(index<0 ||index>=size){
return -1;
}
ListNode cur=head;
//0是虚拟头结点,往后多查一个元素
//这里游标是对链表遍历,而索引的含义是第几个元素,要多遍历一个
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加加后,最后一种index=size的情况变成 index=size-1
//9 10
//遍历找index之前的元素
ListNode pre=head;
//有虚拟头结点,在链表中的第 index 个节点之前添加值为 val 的节点
for (int i = 0; i < index; i++) {
pre=pre.next;
}
ListNode node = new ListNode(val);
node.next=pre.next;
pre.next=node;
size++;
}
//index从0开始,size长度,index=5说明有0-5 6个元素,而size只有5,所以不合法
public void deleteAtIndex(int index) {
if(index>=size || index<0 ){
return;
}
size--;
if(index==0){
head=head.next;
return;
}
ListNode pre=head;
for (int i = 0; i < index; i++) {
pre=pre.next;
}
pre.next=pre.next.next;
}
}
/**
* 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);
*/
//leetcode submit region end(Prohibit modification and deletion)
}
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) {
ListNode pre = null;
ListNode cur = head ;
ListNode temp = null;
while(cur!=null){
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(null,head);
}
private ListNode reverse(ListNode pre,ListNode cur){
//递归终止条件
if(cur==null){
return pre;
}
ListNode temp=null;
temp=cur.next;
cur.next=pre;
return reverse(cur,temp);
}
}