单链表的一些基本操作,插入删除,改变指定位置的元素,反转链表
单链表好在1.任意位置插入删除时间复杂度为O(1),2.没有增容问题,插入一个开辟一个空间
头插
public void addFirst(int data){
Node node = new Node(data);
if(this.head==null) {
this.head = node;
return;
}
node.next = this.head;
this.head = node;
}
尾插
public void addLast(int data){
Node node = new Node(data);
if(this.head==null) {
this.head = node;
return;
}
Node cur = this.head;
while(cur.next!=null) {
cur = cur.next;
}
cur.next = node;
}
按照data域大小插入
public void addOrder(int data) {
Node node = new Node(data);
if(this.head == null) {
this.head = node;
return;
}
if(data<this.head.data) {//如果小于头,那直接插在头前就好了
node.next = this.head;
this.head = node;
}
else {
Node prev = this.head;
if (prev.next == null) {
prev.next = node;
return;
}
while(prev.next.data<data) {//找前驱,同样是为了确定插入的位置。
prev = prev.next;
if(prev.next==null) {//要添加的元素是最大的,直接插在最后即可。
prev.next = node;
return;
}
}
Node p = this.head;
while(p.data<data) {//找到比传过来的data大的就能确定插入的位置了。
p = p.next;
}
node.next = p;
prev.next = node;
}
}
删除
//删除第一次出现关键字为key的节点
public void remove(int key){
if(this.head.data==key) {//删除第一个节点
this.head = this.head.next;
return;
}
Node prev = findPrev(key);
if(prev==null) {
System.out.println("没有这个节点");
return;
}
Node del = prev.next;
prev.next = del.next;
}
删除所有含key节点
public void removeAllKey(int key){
Node prev = this.head;
Node cur = this.head.next;
while(cur!=null) {
if(cur.data==key) {
prev.next = cur.next;
cur = cur.next;
}else {
prev = prev.next;
cur = cur.next;
}
}
if(this.head.data==key) {
this.head = this.head.next;
}
}
按指定位置插入
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data){
if(index<0||index>size()) {
throw new RuntimeException("index位置不合法");
}
if(index==0) {
addFirst(data);
return;
}
if(index==size()) {
addLast(data);
return;
}
//1,让一个引用先走index-1步
Node prev = findIndex(index);
Node node = new Node(data);
node.next = prev.next;
prev.next = node;
}
private Node findIndex(int index) {
Node cur = this.head;
int count = 0;
while(count<index-1) {
cur = cur.next;
count++;
}
return cur;
}
其余辅助代码
//得到单链表的长度
public int size(){
int count = 0;
Node cur = this.head;
while(cur!=null) {
cur = cur.next;
count++;
}
return count;
}
public void display(){
Node cur = this.head;
while(cur!=null) {
System.out.print(cur.data+" ");
cur = cur.next;
}
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
Node cur = this.head;
while(cur!=null) {
if(cur.data==key) return true;
cur = cur.next;
}
return false;
}
改变指定位置的元素
public void change(int pos,int key) {
Node p = this.head;
if (pos==1) {
p.data = key;
return;
}
pos = pos-1;
while (true) {
p = p.next;
pos = pos-1;
if(pos == 0) {
p.data = key;
return;
}
}
}
附一个另外写的递归解决反转链表
public Node reverse(Node1 head) {
if(head==null||head.next==null) {
return head;
}
Node temp = head.next;
Node newHead = reverse(head.next);
temp.next = head;
head.next = null;
return newHead;
}
非递归反转链表
public Node reverseList() {
Node cur = this.head;
Node newHead = null;
Node prev = null;
while(cur!=null) {
Node curNext = cur.next;
if (curNext==null) {
newHead = cur;
}
cur.next = prev;
prev = cur;
cur = curNext;
}
return newHead;
}
完整代码
//节点类
class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
//单链表
public class MyLinedList {
public Node head;
public MyLinedList() {
this.head = null;
}
//头插法
public void addFirst(int data){
Node node = new Node(data);
if(this.head==null) {
this.head = node;
return;
}
node.next = this.head;
this.head = node;
}
//尾插法
public void addLast(int data){
Node node = new Node(data);
if(this.head==null) {
this.head = node;
return;
}
Node cur = this.head;
while(cur.next!=null) {
cur = cur.next;
}
cur.next = node;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data){
if(index<0||index>size()) {
throw new RuntimeException("index位置不合法");
}
if(index==0) {
addFirst(data);
return;
}
if(index==size()) {
addLast(data);
return;
}
//1,让一个引用先走index-1步
Node prev = findIndex(index);
Node node = new Node(data);
node.next = prev.next;
prev.next = node;
}
public void addOrder(int data) {
Node node = new Node(data);
if(this.head == null) {
this.head = node;
return;
}
if(data<this.head.data) {//如果小于头,那直接插在头前就好了
node.next = this.head;
this.head = node;
}
else {
Node prev = this.head;
if (prev.next == null) {
prev.next = node;
return;
}
while(prev.next.data<data) {//找前驱,同样是为了确定插入的位置。
prev = prev.next;
if(prev.next==null) {//要添加的元素是最大的,直接插在最后即可。
prev.next = node;
return;
}
}
Node p = this.head;
while(p.data<data) {//找到比传过来的data大的就能确定插入的位置了。
p = p.next;
}
node.next = p;
prev.next = node;
}
}
private Node findIndex(int index) {
Node cur = this.head;
int count = 0;
while(count<index-1) {
cur = cur.next;
count++;
}
return cur;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
Node cur = this.head;
while(cur!=null) {
if(cur.data==key) return true;
cur = cur.next;
}
return false;
}
//删除第一次出现关键字为key的节点
public void remove(int key){
if(this.head.data==key) {//删除第一个节点
this.head = this.head.next;
return;
}
Node prev = findPrev(key);
if(prev==null) {
System.out.println("没有这个节点");
return;
}
Node del = prev.next;
prev.next = del.next;
}
//找到关键字key的前驱;
private Node findPrev(int key) {
Node prev = this.head;
while(prev.next!=null) {
if(prev.next.data==key) {
return prev;
}
prev = prev.next;
}
return null;
}
//删除所有值为key的节点
public void removeAllKey(int key){
Node prev = this.head;
Node cur = this.head.next;
while(cur!=null) {
if(cur.data==key) {
prev.next = cur.next;
cur = cur.next;
}else {
prev = prev.next;
cur = cur.next;
}
}
if(this.head.data==key) {
this.head = this.head.next;
}
}
public void change(int pos,int key) {
Node p = this.head;
if (pos==1) {
p.data = key;
return;
}
pos = pos-1;
while (true) {
p = p.next;
pos = pos-1;
if(pos == 0) {
p.data = key;
return;
}
}
}
//得到单链表的长度
public int size(){
int count = 0;
Node cur = this.head;
while(cur!=null) {
cur = cur.next;
count++;
}
return count;
}
public void display(){
Node cur = this.head;
while(cur!=null) {
System.out.print(cur.data+" ");
cur = cur.next;
}
}
}
ps:有什么问题可以直接指出来