链表问题(Java实现)

<一从尾到头打印链表>

题目描述

输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
代码如下:
第一种:直接加
/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> res = new ArrayList<>();
        ListNode node = listNode;
        
        while(node != null){
            res.add(0, node.val);
            node = node.next;
        }
        return res;
    }
}

 第二种:用栈的特性想到:

import java.util.ArrayList;
import java.util.Stack;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        Stack<Integer> stack = new Stack<>();  //创建一个堆栈的实例
        while(listNode != 0){                  //判断节点是否存在
            stack.push(linkNode.val);           //存在则节点入栈
            list.add(linkNode.next);            //添加下一个节点
        }
       ArrayList<Integer> list = new ArrayList<>();  //创建一个数组
        while(!stack.isEmpty()){                           //判断栈是否为空
            list.add(stack.pop());                            //将节点的值出栈
        }
        return list;                        //返回节点的值
    }
}

 <二>链表中环的入口结点

题目描述

给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
代码如下:
/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {

    public ListNode EntryNodeOfLoop(ListNode pHead) {
        if(null == pHead || null == pHead.next){
            return null;
        }
        ListNode p1 = pHead;
        ListNode p2 = pHead;
        while(p1 != null && p2 != null){
            p1 = p1.next;
            p2 = p2.next.next;
            if(p1 == p2){
                p1 = pHead;  //无头节点时
                while(p1 != p2){
                    p1 = p1.next;
                    p2 = p2.next;
                }
                if(p1 == p2){
                    return p1;
                }
            }
        }
        return null;
    }
}

<三> 删除链表中重复的结点

题目描述

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5。
代码如下:
/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        ListNode dunny = new ListNode(0);
        dunny.next = pHead;
        
        ListNode p = pHead;
        ListNode q = dunny;
        boolean isDel = false;
        
        while(p != null){
            if(p.next != null && p.val == p.next.val){
                isDel = true;
                p.next = p.next.next;
            }else{
                p = p.next;
                if(isDel){
                    q.next = p;
                    isDel = false;
                }else{
                    q = q.next;
                }
            }
        }
        return dunny.next;
    }
}

 

 

 

 

posted @ 2019-08-19 17:26  离愁i  阅读(256)  评论(0编辑  收藏  举报