import java.util.Scanner; class LinkedList2 { class ListNode { public int val; public ListNode prev; public ListNode next; public ListNode(int val) { this.val = val; } } public ListNode head; public ListNode last; public void addBack(int val) { ListNode Node = new ListNode(val); if (head == null) { head = Node; last = Node; return; } last.next = Node; Node.prev = last; last = Node; } public int size(ListNode head){ int count=0; if(head==null){ return 0; } ListNode tail = head; while(tail.next!=null){ count++; tail = tail.next; } return count; } public ListNode resever(ListNode head,ListNode last){ ListNode Node = new ListNode(0); ListNode tmp = Node; ListNode cur = head; ListNode tail = last; ListNode slow=head; int count=0; ListNode flast=head; while(flast!=null&&flast.next!=null){ count++; flast=flast.next.next; slow =slow.next; } if(size(head)%2==0){ count++; } while(count-->0){ tmp.next=cur; cur.prev=tmp; cur=cur.next; tmp=tmp.next; tmp.next=tail; tail = tail.prev; tail.next.prev=tmp; tmp=tmp.next; } tmp.next =cur; tmp.next=null; return Node.next; } public void display(ListNode head){ if(head==null){ return; } ListNode tail =head; while(tail!=null){ System.out.print(tail.val); if(tail.next!=null){ System.out.print(","); } tail = tail.next; } } } public class Main { public static void getcreate( LinkedList2 head ,String str) { String[] ret = str.split(","); for (int i = 0; i < ret.length; i++) { int a = Integer.parseInt(ret[i]); head.addBack(a); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); LinkedList2 link = new LinkedList2(); LinkedList2 link2 = new LinkedList2(); LinkedList2 link3 = new LinkedList2(); getcreate(link,str); LinkedList2.ListNode tmp= link2.resever(link.head,link.last); link3.display(tmp); } }
双向循环链表,注意判断链表长度奇数偶数。奇数偶数在临界的时候要做处理。