描述(本代码经idea测试无误)
给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头。
数据范围: 0\leq n\leq10000≤n≤1000
要求:空间复杂度 O(1)O(1) ,时间复杂度 O(n)O(n) 。
如当输入链表{1,2,3}时,
经反转后,原链表变为{3,2,1},所以对应的输出为{3,2,1}。
以上转换过程如下图所示:
数据结构代码:
package com.homework1; public class ListNode { int val; ListNode next=null; ListNode previous=null; public ListNode(int val){ this.val=val; } public ListNode(){ } }
具体实现代码:
package com.homework1; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.Stack; public class Solution { public static void main(String[] args) { String result="n"; Scanner scanner=new Scanner(System.in); ListNode head=new ListNode(); do { System.out.println("请输入您要插入的值"); int i = scanner.nextInt(); ListNode listNode=new ListNode(i); //插入链表 insert(head,listNode); System.out.println("是否继续?y/n"); result = scanner.next(); }while ("y".equals(result)); System.out.println("请输入k值:"); int k = scanner.nextInt(); //反转链表 myReverse(head,k); } private static void myReverse(ListNode listNode,int value) { Stack<ListNode> stack=new Stack<>(); ListNode node=null; //获取链表长度 int length=getNodeLength(listNode); //获取尾节点 ListNode endListNode = getEndListNode(listNode); ListNode dummyEndListNode=endListNode; node=endListNode; int m= length - value; int g = length % value; int count=length/value; //获取第(length-value+1)上的节点 int index=length-value+1; ListNode listNode1=getIndexOfValue(listNode,index); //获取第(length-g-value+1)上的节点 int index1=length-g-value+1; ListNode listNode2 = getIndexOfValue(listNode, index1); //获取到第(length-g+1)上的节点 int index2=length-g+1; ListNode listNode3= getIndexOfValue(listNode, index2); ListNode dummyListNode=listNode1; ListNode dummyListNode1=listNode2; ListNode dummyListNode2=listNode3; //入栈 if (length%value==0){ for (int j = 0; j < count; j++) { for (int i=0;i<value;i++){ stack.push(dummyListNode); dummyListNode=dummyListNode.next; } for (int i = 0; i < value*2; i++) { dummyListNode=dummyListNode.previous; } } }else { for (int i = 0; i < g; i++) { stack.push(dummyEndListNode); dummyListNode2=dummyListNode2.previous; } for (int i = 0; i < g; i++) { dummyListNode2=dummyListNode2.previous; } for (int a = 0; a < count; a++) { for (int i=0;i<value;i++){ stack.push(dummyListNode1); dummyListNode1=dummyListNode1.next; } for (int i = 0; i < value*2; i++) { dummyListNode1=dummyListNode1.previous; } } } //出栈 List<ListNode> list=new ArrayList<>(); while (!stack.isEmpty()){ ListNode pop = stack.pop(); list.add(pop); } for (ListNode node1 : list) { System.out.println(node1.val); } } private static ListNode getIndexOfValue(ListNode listNode, int index) { // int length=getNodeLength(listNode); if (listNode==null){ return null; } ListNode node=listNode; for (int i = 0; i <index ; i++) { node=node.next; } return node; } private static int getNodeLength(ListNode listNode) { if(listNode==null){ return 0; } ListNode node=listNode; int i=0; if (node.previous==null){ node=node.next; } while (node!=null){ node=node.next; i++; } return i; } //得到尾节点 private static ListNode getEndListNode(ListNode listNode) { if(listNode==null){ return null; } ListNode node=listNode; while (node.next!=null){ node=node.next; } return node; } //链表插入(双向链表) private static void insert(ListNode head,ListNode node){ if (head==null){ head=node; return; } //尾插法 while (head.next!=null){ head=head.next; } head.next=node; node.previous=head; } }