Java链表常见操作【剑指Offer】03:从尾到头打印链表

题目描述

输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

题解一:递归
 1 /*
 2   在最后一次递归方法返回以后,每一层的递归方法都会做一个arrayList.add(listNode.val)这个操作,
 3  从最后一次到第一次,逆向的调用了后面的方法
 4   */
 5         static ArrayList<Integer> list = new ArrayList<>();
 6         public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
 7             if (listNode != null) {
 8                 printListFromTailToHead(listNode.next);
 9                 list.add(listNode.val);
10             }
11             return list;
12         }
题解二:reverse()方法
1 public static ArrayList<Integer> printListFromTailToHead01(ListNode listNode) {
2         ArrayList<Integer> list = new ArrayList<Integer>();
3         while(listNode != null){
4             list.add(listNode.val);
5             listNode = listNode.next;
6         }
7         Collections.reverse(list);//使用Collections的reverse方法,直接将list反转
8         return list;
9     }

节点结构定义

public static class ListNode {
        int val;
        ListNode next = null;
        ListNode(int val) {
            this.val = val;
        }
    }

从头插入节点

1  public static void insetFromHead(ListNode head,ListNode newNode){
2             newNode.next=head;
3             head = newNode;
4         }

在尾部插入节点

 1 public static void insertFromTail(ListNode head, ListNode newNode){
 2             //如果是个空链表,直接把新节点赋值给head,然后结束,要先判断null的情况
 3             if(head == null){
 4                 head =newNode;
 5                 return;
 6             }
 7         //用temp代替head去遍历找到最后一个节点,一定不要用head自己去遍历,
 8             ListNode temp = head;
 9             while (temp.next!=null){
10                 temp=temp.next;
11             }
12             temp.next=newNode;
13         }

计算链表的长度

1  public  static int length(ListNode head){
2             int len =0;
3             ListNode temp = head;
4             while(temp!=null){
5                 len++;
6                 temp=temp.next;
7             }
8             return len;
9         }

按照顺序输出一个列表

 

1  public static void printList(ListNode head){
2             ListNode temp = head;
3             while(temp != null){
4                 System.out.print(temp.val+" ");
5                 temp = temp.next;
6             }
7             System.out.println();
8         }

从特定位置删除一个节点

 1 public static boolean deleteFromIndex(ListNode head,int index){
 2             if(index<1||index>length(head)){ //先判断是否越界
 3                 return false;
 4             }
 5             if(index ==1){//如果是删除第一个元素,因为直接涉及到了head所以只能单独处理
 6                 head = head.next;
 7                 return true;
 8             }
 9             ListNode curNode = head;
10             //删除顺序为index的node只能将curNode停在index-1的位置
11             for(int curIndex =1;curIndex<index-1;curIndex++){
12                 curNode = curNode.next;
13             }
14             curNode.next=curNode.next.next;
15             return true;
16         }

测试:

 1 public static void main(String[] args) {
 2         Scanner sc = new Scanner(System.in);
 3         int nums = sc.nextInt();
 4         ListNode headNode = new ListNode(1);
 5         for(int i=2;i<=nums;i++){
 6             insertFromTail(headNode,new ListNode(i));
 7         }
 8         printList(headNode);
 9         ArrayList<Integer> list = printListFromTailToHead01(headNode);
10         System.out.println(list);
11         ArrayList<Integer> list1 = printListFromTailToHead(headNode);
12         System.out.println(list1);
13         deleteFromIndex(headNode,3);
14         printList(headNode);
15         int length = length(headNode);
16         System.out.println(length);
17     }

输入:8
输出:
1 2 3 4 5 6 7 8
[8, 7, 6, 5, 4, 3, 2, 1]
[8, 7, 6, 5, 4, 3, 2, 1]
1 2 4 5 6 7 8
7

 

posted @ 2020-02-20 16:08  reset_pc  阅读(237)  评论(0编辑  收藏  举报