链表简介
public class ListNode {
// 结点的值
int val;
// 下一个结点
ListNode next;
// 节点的构造函数(无参)
public ListNode() {
}
// 节点的构造函数(有一个参数)
public ListNode(int val) {
this.val = val;
}
// 节点的构造函数(有两个参数)
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
1、节点类
节点类ListNode有两个成员变量,一个是节点的值,一个是指针,指向下一个节点。通过指针串联成链表。
2、链表的存储方式
链表是由多节点组成,它的存储方式是通过指针域的指针链接在内存中的各个节点。所以它的节点在内存中不是连续分布的,而是散乱分布在内存的某个地址上,分配机制取决于操作系统的内存管理。
3、链表和数组的适用场景
数组适用于数据量固定,频繁查询(地址连续),较少增删(移动其他元素位置)。
链表适用于数据量不固定,频繁增删(仅仅调用指针指向即可),较少查询(地址不连续)。
即不严谨情况下,增删多用链表,查询多用数组。
203.移除链表元素
1、代码
/**
* 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;
}
while(head != null && head.val == val){
head = head.next;
}
//非头节点返回的情况
ListNode newHead = head;
while(newHead != null){//指针才能和空做判断
//判断数值之前先进行非空判断,否则会发生编译错误
while(newHead.next !=null && newHead.next.val == val){
newHead.next = newHead.next.next;
}
//if改成while,解决了连续val的情况
//因为下一个节点需要删除的时候需要指针在上一个节点的帮助
newHead = newHead.next;
}
return head;
}
}
分析
(1)每次对需要操作的节点进行操作的时候,都要进行非空判定,直接操作会 编译报错!
(2)移除元素分为两种情况:
当要移除的元素在头节点时候,我们用如下代码移除元素:
head = head.next;
当要移除的元素不在头节点的时候,我们用如下代码移除元素:
ListNode newHead = head;
while(newHead != null){
while(newHead.next !=null && newHead.next.val == val){
newHead.next = newHead.next.next;
}
newHead = newHead.next;
}
(3)移除节点本质上是通过该节点(cur)的上一个节点(pre)指向下一个节点(cur.next),所以为了应对头节点的例外情况,我们可以自己设定一个虚拟头节点指向头节点,这样就将移除节点的方式统一化。
2、改进代码
/**
* 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){
return head;
}
ListNode dummy = new ListNode(-1,head);
ListNode cur = head;
ListNode pre = dummy;
while(cur != null){
if(pre.next.val == val){
pre.next = cur.next;
}else{
pre = cur;
}
cur = cur.next;
}
return dummy.next;
//头节点可能已经被删除,所以虚拟节点的下一个节点才是真正的头节点
//引入cur是为了遍历每个节点
}
}
分析
(1)此时cur用来遍历节点,pre用来一直作为cur的上一个节点,用来对cur节点进行移除操作。
(2)当程序执行完之后,由于头节点的值可能为val,在执行时已经被删除,所以要返回的头节点是dummy.next。
707.设计链表
1、代码
class ListNode{
int val;
ListNode next;
ListNode(){}
ListNode(int val){
this.val = val;
}
}//构造了一个节点
class MyLinkedList {
int size;//链表的长度
ListNode head;//虚拟头节点
public MyLinkedList() {
size = 0;//只有一个虚拟头节点,长度为0
head = new ListNode(0);//创建一个虚拟头节点
}
public int get(int index) {
if(index < 0 || index > size - 1){
return -1;
}
ListNode cur = head;
for(int i=0; i<=index; i++){//index在这里类似于数组长度的意思
cur = cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
addAtIndex(0,val);
}
public void addAtTail(int val) {
addAtIndex(size,val);//有虚拟头节点,所以不是size-1
}
public void addAtIndex(int index, int val) {
if(index < 0){
index = 0;
}
if(index > size){
return;
}
size++;
ListNode cur = head;
ListNode newNode = new ListNode(val);
for(int i=0; i<index; i++){
cur = cur.next;
}
newNode.next = cur.next;
cur.next = newNode;
}
public void deleteAtIndex(int index) {
if(index < 0 || index > size - 1 ){
return ;
}
size--;
ListNode cur = head;
for(int i=0; i<index; i++){
cur = cur.next;
}
if(cur.next != null){
cur.next = cur.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);
*/
2、分析
(1)因为链表基于节点,所以设计链表要建立一个节点类。
(2)初始化链表,由于本次使用虚拟头节点设计链表,所以size的值置为0,并且创建一个值为0的虚拟头节点。
(3)查询第index个节点的值:比如链表的长度为3,我们要查第3个节点,则index对应的链表下标为 0,1,2 中的2(类似于数组)。这是由于index也是从0开始,所以我们要遍历的范围是(0,index - 1),又因为我们又在头节点之前添加了一个虚拟头节点(i=0),所以在遍历的时候我们应该多遍历一次,即(0,index)。
(4)在链表的第index个节点之前添加值:由于是增加节点,所以在判定完临界情况之后我们需要将size++,然后(i <index)遍历到该节点的上一个节点(因为有虚拟头节点i=0)之后,我们通过调整指针朝向添加节点。
(5)删除节点同(4)。
206.反转链表
1、代码
/**
* 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;
}
}
分析
(1)本题最重要的是链表翻转的动态过程(草图,节点不变,调整指向),然后利用双指针的方法进行链表翻转,用一个temp指针暂存链表剩下的节点。
(2)pre在一开始作为一个虚拟节点。
2、递归写法
/**
* 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;
// if(cur != null){
temp = cur.next;
cur.next = pre;
// }
return reverse(cur,temp);
}
}
分析
注意点:对于指针的操作要进行判空。