1.理解Java是如何构造出链表的
链表的实现形式很像方法的递归,结点套结点,每个结点包含数据域和指向下一个结点的引用。
下一个结点又包含它的数据域和下下个结点的引用。以此,就是一条单链表。
public class ListNode{
public int val;
public ListNode next;
ListNode(int x){
val = x;
next = null;
}
}
2.链表添加元素,首部、中部、尾部分别会有什么问题,该如何处理?
在首部添加元素只需要newNode.next = head,之后遍历新链表就从newNode一路next向下。
在中部添加元素:4 15 7 40,在7前面插入, 当cur.next==node(7)就停下来,此时cur.val==15,先new.next=node(15).next 再node(15).next=new 顺序不能错,先后再前。
在尾部添加元素只需要last.next = newNode,用中部添加元素的方法也可实现。
/**
* 遍历链表
* @param head 头结点
* @return 返回链表长度
*/
public static int getListLength(ListNode head){
int length = 0;
ListNode node = head;
while (node != null){
length++;
node = node.next;
}
return length;
}
/**
* 链表插入 考虑首部,中部,尾部
* @param head 链表头结点
* @param nodeInsert 待插入结点
* @param position 待插入位置,从 1 开始
* @return 插入后得到的链表头结点
*/
public static ListNode insertNode(ListNode head,ListNode nodeInsert,int position){
if(head == null){
//认为待插入的结点就是链表的头结点,也可以抛出不能插入的异常
return nodeInsert;
}
//已经存放的元素个数
int size = getListLength(head);
if(position > size + 1 || position < 1){
System.out.println("位置参数越界");
return head;
}
//表头插入
if(position == 1){
nodeInsert.next = head;
head = nodeInsert;
return head;
}
//4 15 7 40
//在7前面插入, 当cur.next==node(7)就停下来,此时cur.val==15
//先new.next=node(15).next 再node(15).next=new 顺序不能错,先后再前
ListNode pNode = head;
int count = 1;
//position在上面被size限制了,不用考虑pNode == null
while (count < position - 1){
pNode = pNode.next;
count++;
}
nodeInsert.next = pNode.next;
pNode.next = nodeInsert;
return head;
}
3.链表删除元素,首部、中部、尾部分别会有什么问题,该如何处理?
删除首部元素只需要head = head.next,head向前移动一次之后,原来的结点不可达,会被JVM回收。
删除尾部元素只需要把 -2 位置的结点current.next = null , 这样 -1 位置的结点就不可达,会被JVM回收。
删除中部结点需要把current.next = current.next.next,这样被删结点就不再会被访问到。
/**
* 删除结点 考虑首部,中部,尾部
* @param head 链表头位置
* @param position 删除结点位置,从 1 开始
* @return 删除后的链表头结点
*/
public static ListNode deleteNode(ListNode head,int position){
if(head == null){
return null;
}
int size = getListLength(head);
if(position > size || position < 1){
//删除最后一个结点只需要遍历到 -2 位置 所以只需要size而不是size+1
System.out.println("输入的位置参数有误");
return head;
}
//删除头结点只需要head = head.next; head向前移动一次后,原来的结点不可达,会被JVM回收、
//删除尾结点只需要cur.next=null; 同理不可达,会被JVM回收
if(position == 1){
//curNode就是链表的新头结点
return head.next;
}else{
ListNode preNode = head;
int count = 1;
while (count < position -1){
preNode = preNode.next;
count++;
}
ListNode curNode = preNode.next;
preNode.next = curNode.next;
}
return head;
}
4.双向链表是如何构造的,如何实现元素的插入和删除。
双向链表是在单链表的基础上加上一个prev指向前驱结点。一个结点包含当前数据域,指向上一个结点的引用和指向下一个结点的引用。
public class DoublyLinkList {
class DoubleNode{
public int data; //数据域
public DoubleNode next; //指向下一个结点
public DoubleNode prev; //指向上一个结点
public DoubleNode(int data){
this.data = data;
}
//打印结点的数据域
public void displayNode(){
System.out.println("{" + data + "}");
}
}
private DoubleNode first;
private DoubleNode last;
//初始化双向链表
public DoublyLinkList(){
first = null;
last = null;
}
//从头部开始打印
public void displayForward(){
System.out.println("List(first---->last):");
DoubleNode current = first;
while (current != null){
current.displayNode();
current = current.next;
}
System.out.println();
}
//从尾部开始打印
public void displayBackward(){
System.out.println("List(last---->first):");
DoubleNode current = last;
while (current != null){
current.displayNode();
current = current.prev;
}
System.out.println();
}
/**
* 头部插入
* @param data
*/
public void insertFirst(int data){
DoubleNode newDoubleNode = new DoubleNode(data);
if (last == null){
//如果第一个结点是空
last = newDoubleNode;
} else {
//如果不是第一个结点的情况
//将未插入前的头结点的前驱指向新结点
first.prev = newDoubleNode;
}
newDoubleNode.next =first;
first = newDoubleNode;
}
/**
* 尾部插入
* @param data
*/
public void insertLast(int data){
DoubleNode newDoubleNode = new DoubleNode(data);
if(last == null){
//如果第一个结点是空
first = newDoubleNode;
}else {
//如果不是第一个结点的情况
//将未插入前的尾结点的后驱指向新结点
//将新结点的前驱指向未插入前的尾结点
newDoubleNode.prev = last;
last.next = newDoubleNode;
}
//新插入尾结点,所以将last指向新结点
last = newDoubleNode;
}
/**
* 在某节点后插入新结点
* @param key 某节点
* @param data 新结点
*/
public void insertAfter(int key,int data){
DoubleNode newDoubleNode = new DoubleNode(data);
DoubleNode current = first;
while (current != null && current.data != key){
current = current.next;
}
//若当前结点current为空
if(current == null){
if (last == null){
first = newDoubleNode;
last = newDoubleNode;
}else {
//找不到key值,则在链表尾部插入一个新结点
last.next = newDoubleNode;
newDoubleNode.prev = last;
last = newDoubleNode;
}
}else {
//找到key值,分两种情况
if(current == last){
//key值与最后结点的data相等
newDoubleNode.next = null;
last = newDoubleNode;
}else {
//两结点中插入
newDoubleNode.next = current.next;
current.next.prev = newDoubleNode;
}
}
current.next = newDoubleNode;
newDoubleNode.prev = current;
}
/**
* 删除头结点
* @return 返回删除的结点
*/
public DoubleNode deleteFirst(){
DoubleNode temp = first;
if(first.next == null){
//若链表只有一个结点,删除后链表为空,将last指向null
last = null;
}else {
// 若有两个及以上结点,头部删除,则first.next将变成第一个结点,其前驱prev变成null
first.next.prev = null;
}
//将first.next赋给first
first = first.next;
//返回删除的结点
return temp;
}
/**
* 从尾部删除结点
* @return 返回删除的结点
*/
public DoubleNode deleteLast(){
DoubleNode temp = last;
if(first.next == null){
//如果链表只有一个结点,删除后为空链表,last指向null
first = null;
}else {
//将上一个结点的next指向null
last.prev.next = null;
}
last = last.prev;
//返回删除的结点
return temp;
}
/**
* 删除指定结点
* @param key 指定结点
* @return
*/
public DoubleNode deleteKey(int key){
DoubleNode current = first;
//遍历链表寻找该值所在结点
while (current != null && current.data != key){
current = current.next;
}
if(current == null){
//若当前结点current为null则返回null
return null;
}else {
if(current == first){
//如果current 是第一个结点,则将first指向它,该结点的prev指向null,其余不变
first = current.next;
current.next.prev = null;
}else if(current == last){
//如果current是最后一个结点
last = current.prev;
current.prev.next = null;
}else {
//当前结点的上一个结点的next应指向当前节点的下一个结点
current.prev.next = current.next;
//当前结点的下一个结点的prev应指向当前结点的上一个结点
current.next.prev = current.prev;
}
}
return current;
}
}