public class ListNode {
/**
* @param args
* 声明一个链表
*/
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;
}
//链表的遍历操作
int ListLength(ListNode headNode){
int length=0;
ListNode currentNode=headNode;
while (currentNode!=null) {
length++;
headNode=currentNode.getNext();
}
return length;
}
//在单链表中插入节点
ListNode InsertInLinkedList(ListNode headNode,ListNode nodeInsert,int position){
//链表为空
if (headNode==null) {
return nodeInsert;
}
//如果传入的参数越界
int size=ListLength(headNode);
if (position>size+1||position<1) {
System.out.println("非法的参数position");
return headNode;
}
//如果插入的位置为开头
if (position==1) {
nodeInsert.setNext(headNode);
}
//插入位置为中间或者结尾
else {
ListNode preNode=headNode;
int count=1;
//遍历,找到所在位置
while (count<position-1) {
preNode=preNode.getNext();
count++;
}
ListNode currentNode=preNode.getNext();
nodeInsert.setNext(currentNode);
preNode.setNext(nodeInsert);
}
return headNode;
}
//删除链表的节点
ListNode DeleteNode(ListNode headNode,int position){
if (position==1) {
ListNode currentNode=headNode.getNext();
headNode=null;
return currentNode;
}else {
ListNode preNode=headNode.getNext();
int count=1;
while (count<position) {
preNode=preNode.getNext();
count++;
}
ListNode currentNode=preNode.getNext();
//指向删除的节点的下个节点
preNode.setNext(currentNode.getNext());
currentNode.setNext(null);
}
return headNode;
}
//删除单链表
void DeleteLinkedList(ListNode head){
ListNode auxListNode,iterator=head;
while (iterator!=null) {
auxListNode=iterator.getNext();
iterator=null;
}
}
//Question1.在有序的链表中插入一个节点
ListNode insertSorted(ListNode head,ListNode newNode){
ListNode current=head;
ListNode temp=null;
if (head==null) {
return newNode;
}
while (current!=null&¤t.getData()<newNode.getData()) {
temp=current;
current=current.getNext();
}
newNode.setNext(current);
head.setNext(temp);
return head;
}
/*
* Q2.假设两个单向链表在某个节点相交后,成为一个单向链表。List1和List2在相交前的节点数
* 分别为n和m。设计算法找到两个链表的合并点。并使时间复杂度最低。
* 以下算法时间复杂度为O(max(m,n))
* 先让较长的移动len个节点
*/
ListNode Q2(ListNode list1,ListNode list2){
int L1=0;int L2=0;int len=0;
ListNode head1=list1,head2=list2;
while (head1!=null) {
L1++;
head1=head1.getNext();
}
while (head2!=null) {
L2++;
head2=head2.getNext();
}
//保证head1是较长list的节点
if (L1<L2) {
head1=list2;
head2=list1;
len=L2-L1;
}else {
head1=list1;
head2=list2;
len=L1-L2;
}
for (int i = 0; i < len; i++)
head1=head1.getNext();
while (head1!=null&&head2!=null) {
if (head1==head2)
System.out.println(head1.getData());
head1=head1.getNext();
head2=head2.getNext();
}
return null;
}
/*
* Q3.如何把两个有序链表合并成一个新的有序链表、、、递归求解、、、
*
*/
ListNode MergeList(ListNode a,ListNode b){
ListNode result=null;
if (a==null) {
return b;
}if (b==null) {
return a;
}
if (a.getData()<b.getData()) {
result=a;
result.setNext(MergeList(a.getNext(), b));
}else {
result=b;
result.setNext(MergeList(b.getNext(), a));
}
return result;
}
/*
* Q4.如何逐对逆置链表?例如1-2-3-4-X,2-1-4-3-X
*
*/
//递归求解
ListNode ReNode(ListNode list){
ListNode temp;
if (list==null||list.next==null) {
System.out.println("error");;
}else {
temp=list.next;
list.next=temp.next;
temp.next=list;
list=temp;
ReNode(list.next.next);
}
return list;
}
}
链表中的一些算法,Java实现
最新推荐文章于 2024-08-06 17:42:20 发布