单链表构造方法
Java
JVM有栈区和堆区
栈区:存引用,就是指向实际对象的地址。。
堆区:存的是创建的对象。
定义
规范的链表定义
public class ListNode{
private int data;
private ListNode next;
public ListNode(int data){
this.data=data;
}
public int getData(){
return data;
}
public void setData(int data){
this.data=data;
}
public ListNode getNext(){
return next;
}
public void setNext(ListNode next){
this.next=next;
}
}
LeetCode算法题中常用
public class ListNode {
public int val;
public Node next;
Node(int x) {
val = x;
ext = null;//作用不大,写了更标准
}
}
遍历
public static int getListLength(Node head){
int length=0;
Node node=head;
while(node!=null){
length++;
node=node.next;
}
return length;
}
插入
public static Node insertNode(Node head,Node nodeInsert,int position){
//将节点插入一个空链表
if(head==null){
return nodeInsert;
}
//越界
int size=getLength(head);
if(position>size+1||position<1){
System.out.println("位置参数越界");
return head;
}
//表头插入
if(position)==1){
nodeInsert.next=head;
head=nodeeInsert;
return head;
}
//其他位置插入
Node pNode=head;
int count=1;
while(count<position-1){
pNode=pNode.next;
count++;
}
nodeInsert.next=pNode.next;
pNode.next=nodeinsert;
return head;
}
删除
public static Node deleteNode(Node head,int position){
//排除空链表
if(head==null){
return null;
}
//越界
int size=getLength(head);
if(position >size+1||position <1){
System.out.println("位置参数越界");
return head;
}
//删除节点
if(position==1){
head=head.next;
}else{
Node pNode=head;
int count =1;
while(count<position-1){
count++;
pNode=pNode.next;
}
Node delNode=pNode.next;
pNode.next=delNode.next;
}
return head;
}
双向链表
Java
结点
class DoubleNode{
public int data;
public DoubleNode pre;
public DoubleNode next;
public DoubleNode(int data){
this.data=data;
}
}
结构&遍历
public class DoubleLinkList{
private DoubleNode first;
private DoubleNode last;
public DoubleLinkList(){
first=null;
last=first;
}
//从头遍历
public void displayForward(){
DoubleNode current=first;
while(current!=null){
current.displayNode();
current=current.next;
}
}
//从尾部遍历
public void displayBackward(){
DoubleNode current=last;
while(current!=null){
current.displayNode();
current=current.pre;
}
}
}
插入
从头插入
DoublyLinkList doublyLinkList = new DoublyLinkList();
doublyLinkList.insertFirst(20);
public void insertFirst(int data) {
DoubleNode newDoubleNode = new DoubleNode(data);
if (isEmpty()) {
last=newDoubleNode;//列表初始化时first,last都为null
}else{
first.pre=newDoubleNode;
newDoubleNode.next=first;
}
first=newDoubleNode;//让该结点称为第一个节点
}
从尾插入
public void insertLast(int data) {
DoubleNode newDoubleNode = new DoubleNode(data);
if (isEmpty()) {
first=newDoubleNode;//列表初始化时first,last都为null
}else{
newDoubleNode.pre=last;
last.next=newDoubleNode;
}
last=newDoubleNode;//让该结点称为最后一个节点
}
从某个值为key的节点后面插入
public void insertAfter(int key, int data){
DoubleNode newDoubleNode = new DoubleNode(data);
DoubleNode current = first;
//找值为key的节点
while ((current != null) && (current.data != key)){
current = current.next;
}
//若current==null,要么链表为空,要么没有值为key的数
if (current == null){
//根据题意来写...
}else{
//这里还有可能key值是最后一个数,写法参考上面,这里不再考虑
newDoubleNode.next=current.next;
current.next.prev = newDoubleNode;
newDoubleNode.pre=current;
current.next=newDoubleNode;
}
}
删除
删除头结点
public DoubleNode deleteFirst(){
//如果只有一个节点。需要多考虑一个变量last
if (first.next == null){
last=null;
}else{
first.next.pre=null;
}
first=first.next;
}
删除尾结点文章来源:https://www.toymoban.com/news/detail-608647.html
public DoubleNode deleteLast(){
//如果只有一个节点。需要多考虑一个变量first
if (first.next == null){
first=null;
}else{
last.pre.next=null;
}
last=last.pre;
}
按值删除文章来源地址https://www.toymoban.com/news/detail-608647.html
public DoubleNode deleteKey(int key){
DoubleNode current = first;
//寻找key值所在的节点
while (current != null && current.data != key){
current = current.next;
}
//若current==null,要么链表为空,要么没有值为key的数
if (current == null){
//根据题意来写...
return null;
}else{
//这里还有可能key值是第一个或者最后一个数,写法参考上面,这里不再考虑
current.next.prev = current.pre;
current.pre.next=current.next;
}
}
到了这里,关于编程导航算法通关村第一关|青铜|链表基础的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!