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 cur = head;
while(cur.next != null){
if(cur.next.val == val){
cur.next = cur.next.next;
if(cur.next == null && head.val != val){
return head;
}
else if(cur.next == null && head.val == val){
return head.next;
}
}else{
cur = cur.next;
}
}
if(head.val == val)head = head.next;
return head;
}
}
改进
方式1
逻辑混乱的原因在于没有在开始的时候考虑清楚节点的种类,一类是头节点,一类是普通节点,这两种节点值等于val的删除操作不一样(头节点head = head.next,普通节点 cur.next = cur.next.next),分开写代码处理会更清晰。
/**
* 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;
}
ListNode cur = head;//由于上一个循环的存在,这里确保cur的值不会是val。
while(cur != null && cur.next != null){//cur.next 不存在的话循环结束,cur的值不会是val。
if(cur.next.val == val){
cur.next = cur.next.next;
}else{
cur = cur.next;//不相等cur往后挪,cur的值始终不会等于val
}
}
return head;
}
}
方式2
添加虚拟头节点(dummy 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 dummyHead = new ListNode();
dummyHead.next = head;
ListNode cur = dummyHead;//这里跟上一个方法一样保证cur的值不会等于val
while(cur.next != null){//这里和上面方法不一样 (cur != null && cur.next != null)
//这里cur不会是空,是dummy head
if(cur.next.val == val){
cur.next = cur.next.next;
}else{
cur = cur.next; //循环的条件是cur.next不为空,cur不会空。
}
}
return dummyHead.next;
}
}
707设计链表
自己写的
class LinkNode{
int val;
LinkNode next;
LinkNode(int val){
this.val = val;
}
}
class MyLinkedList {
int size;
LinkNode head ;//虚拟头节点
public MyLinkedList() {
size = 0;
head = new LinkNode(0);
//初始化
}
public int get(int index) {
//根据索引值获取元素
if(index > size - 1){return -1;}
LinkNode cur = head;
for(int i = 0;i <= index;i++){
cur = cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
LinkNode add = new LinkNode(val);
add.next = head.next;
head.next = add;
size++;
}
public void addAtTail(int val) {
LinkNode cur = head;
while(cur.next != null){
cur = cur.next;
}
cur.next = new LinkNode(val);
size++;
}
public void addAtIndex(int index, int val) {
if(index > size ){}
else{
LinkNode pre = head;
for(int i = 0;i < index;i++){
pre = pre.next;
}
LinkNode add = new LinkNode(val);
add.next = pre.next;
pre.next = add;
size++;
}
//在索引值后添加新元素
}
public void deleteAtIndex(int index) {
//若索引值有值,删除该节点
if(index > size - 1 || index < 0){}
else{
LinkNode pre = head;
if(index == 0){
head.next = head.next.next;
}else{
for(int i = 0;i < index;i++){//index是索引位置,要比size小1.循环pre指向的与当前i一致。
pre = pre.next;
}
pre.next = pre.next.next;
}
size--;
}
}
}
改进
1、在头节点和尾节点添加数据可以调用addAtIndex方法。
双链表
206反转列表
https://leetcode.cn/problems/reverse-linked-list/
双指针
/**
* 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) {
ListNode pre = null;
ListNode cur = head;
return reverse(cur,pre);
}
public ListNode reverse(ListNode cur, ListNode pre){
if(cur == null)return pre;
ListNode temp = cur.next;
cur.next = pre;
return reverse(temp,cur);
}
}