如:1,2,3,4,5 输入2 结果为 2,1,4,3,5
条件:单链表
思路:先将要逆序的元素从原链表中去掉,放在某一个数据结构中,再按照逆序插入原链表。单链表必须要考虑的是某个节点的父节点该如何找到,当然得考虑到右重复值得情况,对于,头节点来说,是没有父节点的,所以,每次插入都是在链表的头节点的前面插入。我是从头部开始去掉的,那么这第一个去掉的元素(elem)再插入链表应该是最先插入,即先进先出。当第一次逆序结束后,第二次再去掉后面的某些部分,考虑到找插入点的父节点的麻烦,所以我选择的是从某个元素(A)的的后面插入,而这里的A我可以从头次插入得到,即最先从队列中出来的那个元素(elem),删除的元素也是从elem开始往后删,要逆序输出,得先进来的元素后出去,即栈的性质
程序用到:队列(在第一次逆序时,用队列的先进先出性质),栈(在其它逆序输出时,用到栈的先进后出)
import java.util.Scanner;
public class ListReverse{
public boolean isEmpty(){
return head==null;
}
public void insert(MyElem myElem){
if (isEmpty()) {
head=myElem;
}else{
tail.next=myElem;
}
tail=myElem;
}
public void insertHead(MyElem myElem){
if (isEmpty()) {
head=myElem;
tail=myElem;
tail.next=null;
return;
}
myElem.next=head;
head=myElem;
}
public void insertAfterPosition(MyElem pos,MyElem newElem){
if (pos.next==null) {
pos.next=newElem;
newElem.next=null;
tail=newElem;
return;
}
MyElem temp=pos.next;
pos.next=newElem;
newElem.next=temp;
}
public MyElem removeAfterPosition(MyElem pos){
if (pos.next==null) {
throw new Error("wrong,no next");
}
MyElem temp=pos.next;
if (pos.next.next==null) {
pos.next=null;
}else{
pos.next=temp.next;
}
return temp;
}
public MyElem removeHead(){
if(isEmpty()){
return null;
}
MyElem temp=head;
head=head.next;
return temp;
}
public void printMyList(){
MyElem temp=head;
while(temp!=null){
System.out.print(temp.elem+" ");
temp=temp.next;
}
System.out.println();
}
public int getLengthOfMyList(){
int count=0;
MyElem temp=head;
while(temp!=null){
count++;
temp=temp.next;
}
return count;
}
public void reverse(int k){
int len=getLengthOfMyList();
if(k>len|| k<=1){
return;
}
int count=1;
MyElem firstDotPre=null;//in next recyle ,this will be the first elemt.pre;
MyQueue mq=new MyQueue();
MyStack ms=new MyStack(k);
while(len/(k*count)!=0){
if (count==1) {
//首次从头部逆序
//删
for (int i=0;i<k ;i++ ) {
mq.enQueue(removeHead());
}
//存
for (int i=0;i<k ;i++ ) {
insertHead(mq.deQueue());
if (i==0) {
firstDotPre=head;
}
}
}else{
//其它地方逆序
//删
for(int i=0;i<k;i++){
ms.push(removeAfterPosition(firstDotPre));
}
//here wll print more elem than it should be ,but it will not be a promblem
// mq.printMyQueue();
//存
MyElem temp=null;
for (int i=0;i<k ; i++) {
temp=ms.pop();
insertAfterPosition(firstDotPre,temp);
firstDotPre=temp;
}
}
count++;
}
}
public static void main(String[] args){
ListReverse lr=new ListReverse();
for (int i=0;i<5 ;i++ ) {
lr.insert(new MyElem(i,null));
}
lr.printMyList();
// lr.removeAfterPosition(lr.head);
// lr.insertInPosition(lr.tail,new MyElem(10,null));
Scanner scan=new Scanner(System.in);
int k=scan.nextInt();
lr.reverse(k);
lr.printMyList();
}
MyElem head=null;
MyElem tail=null;
}
//单个节点元素
class MyElem{
int elem;
MyElem next;
public MyElem(MyElem next){
this.next=next;
}
public MyElem(int elem , MyElem next){
this.elem=elem;
this.next=next;
}
}
//队列
class MyQueue{
MyElem head=null;
MyElem tail=null;
public boolean isEmpty(){
return head==null;
}
public void enQueue(MyElem myElem){
if(isEmpty()){
head=myElem;
}else{
tail.next=myElem;
}
tail=myElem;
}
public MyElem deQueue(){
if (isEmpty()) {
throw new Error("no element");
}
MyElem temp=head;
head=head.next;
return temp;
}
public void printMyQueue(){
MyElem temp=head;
while(temp!=null){
System.out.print(temp.elem+" ");
temp=temp.next;
}
System.out.println();
}
}
//栈
class MyStack{
MyElem[] elems=null;
public MyStack(int k){
elems=new MyElem[k];
}
int topOfStack=-1;
public boolean isEmpty(){
return topOfStack==-1;
}
public boolean isFull(){
return topOfStack==10;
}
public void push(MyElem elem){
topOfStack++;
if(!isFull()){
elems[topOfStack]=elem;
}
}
public MyElem pop(){
if (!isEmpty()) {
MyElem temp=elems[topOfStack];
topOfStack--;
return temp;
}else{
throw new Error("wrong ,no elem in stack ");
}
}
}