笔试java--链表

package java_basic;

import java.util.ArrayList;
import java.util.Stack;

/**
 * 从尾到头遍历链表 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList
 * 
 * @author Administrator
 */
class ListNode {// 单链表节点构建   //不能是public
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}

public class java_链表_1 {

    static ListNode head = null;// 创建一个头节点

    public static void main(String[] args) {
        addNode(1);
        addNode(2);
        addNode(3);
        addNode(4);
        addNode(5);
        addNode(6);
        //逆序打印
        System.out.println("输出逆序链表:");
        ArrayList<Integer> list = printListFromTailToHead(head);// 没有改变链表的顺序
        System.out.println(list);
        // 反转
        ListNode invListNode= ReverseIteratively_ws(head);//  改变顺序
        System.out.println(invListNode.val);
        System.out.println(invListNode.next.val);
        printListReversely(invListNode);//没有改变顺序,只是用递归方式打印
//        System.out.println("先逆置,再输出:");
        
    }

    // 队列和栈是一对好基友,从尾到头打印链表,当然离不开借助栈的帮忙啦
    // 所以,先把链表里的东西,都放到一个栈里去,然后按顺序把栈里的东西pop出来,就这么简单
    public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        Stack<Integer> stack = new Stack<Integer>();
        while (listNode != null) {
            stack.push(listNode.val);
            listNode = listNode.next;
        }
        ArrayList<Integer> list = new ArrayList<Integer>();
        while (!stack.isEmpty()) {
            list.add(stack.pop());
        }
        return list;
    }

    // input
    public static void addNode(int d) {
        // System.out.println("add-begin:"+d);
        ListNode newNode = new ListNode(d);
        if (head == null) {
            head = newNode;
            //return;
            // ws加的,如果不加return,那么,执行tmp.next=newNode的时候,会将next指针指向自己,
            //或是加个else,呀么执行head==null时的,要么执行head!=null的条件
        } else {
            ListNode tmp = head;
            while (tmp.next != null) {
                // System.out.println("tmp.next != null");
                // System.out.println("tmp:"+tmp);
                // System.out.println("tmp.next:"+tmp.next);
                tmp = tmp.next;
            }
            tmp.next = newNode;
            // System.out.println("tmp.next.val:"+ tmp.next.val);
        }
        // System.out.println("add-end:"+d);
    }

    // delete
    public boolean deleteNode(int index) {
        if (index < 1 || index > length()) {
            return false;// 如果当前index在链表中不存在
        }
        if (index == 1) {// 如果index指定的是头节点
            head = head.next;
            return true;
        }
        int i = 2;
        ListNode preNode = head;// 前一个节点(从头节点开始)
        ListNode curNode = preNode.next;// 当前节点
        while (curNode != null) {
            if (i == index) {
                preNode.next = curNode.next;// 删除当节点,前节点连接到下节点
                return true;
            }
            preNode = curNode;
            curNode = curNode.next;
            i++;
        }
        return false;
    }

    // 返回节点长度

    public int length() {
        int length = 0;
        ListNode tmp = head;
        while (tmp != null) {
            length++;
            tmp = tmp.next;
        }
        return length;
    }

    // 链表反转
    public ListNode ReverseIteratively(ListNode head) {
        ListNode pReversedHead = head;
        ListNode pNode = head;
        ListNode pPrev = null;
        while (pNode != null) {
            ListNode pNext = pNode.next;
            if (pNext == null) {
                pReversedHead = pNode;
            }
            pNode.next = pPrev;//     
            pPrev = pNode;//    
            pNode = pNext;//    
        }
        this.head = pReversedHead;
        return this.head;
    }
    
    // 链表反转-ws
    public static  ListNode ReverseIteratively_ws(ListNode head) {
        ListNode pNext=null;
        ListNode pNewHead = null;
        ListNode pReversedHead = null;
        ListNode pNode = head;
        while (pNode != null) {
            pNext = pNode.next;
            //   pNode是从原链表拿下来,并放到新链表的那个被转移的节点
            pNode.next = pNewHead;//   ws   把原来新链表的头结点 拼到pNode上
            pNewHead = pNode;// ws   pNewHead头衔易主,pNewHead是pNewHead,即新链表的头节点点,
            pNode = pNext;//ws    pNode的头衔易主,pNext这个元素称为新的pNode,,,,pNode是原来链表的头节点
        }
        head=pNewHead;
        return head;
    }

    // 查找单链表的中间节点

    public ListNode SearchMid(ListNode head) {
        ListNode p = this.head, q = this.head;
        while (p != null && p.next != null && p.next.next != null) {
            p = p.next.next;
            q = q.next;
        }
        System.out.println("Mid:" + q.val);
        return q;
    }

    // 查找倒数 第k个元素

    public ListNode findElem(ListNode head, int k) {
        if (k < 1 || k > this.length()) {
            return null;
        }
        ListNode p1 = head;
        ListNode p2 = head;
        for (int i = 0; i < k; i++)
            // 前移k步
            p1 = p1.next;
        while (p1 != null) {
            p1 = p1.next;
            p2 = p2.next;
        }
        return p2;
    }

    // 排序

    public ListNode orderList() {
        ListNode nextNode = null;
        int tmp = 0;
        ListNode curNode = head;
        while (curNode.next != null) {
            nextNode = curNode.next;
            while (nextNode != null) {
                if (curNode.val > nextNode.val) {
                    tmp = curNode.val;
                    curNode.val = nextNode.val;
                    nextNode.val = tmp;
                }
                nextNode = nextNode.next;
            }
            curNode = curNode.next;
        }
        return head;
    }

    // 从尾到头输出单链表,采用递归方式实现

    public  static void printListReversely(ListNode pListHead) {
        if (pListHead != null) {
            printListReversely(pListHead.next);
            System.out.println("printListReversely:" + pListHead.val);
        }
    }

    // 判断链表是否有环,单向链表有环时,尾节点相同

    public boolean IsLoop(ListNode head) {
        ListNode fast = head, slow = head;
        if (fast == null) {
            return false;
        }
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                System.out.println("该链表有环");
                return true;
            }
        }
        return !(fast == null || fast.next == null);
    }

    // 找出链表环的入口

    public ListNode FindLoopPort(ListNode head) {
        ListNode fast = head, slow = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast)
                break;
        }
        if (fast == null || fast.next == null)
            return null;
        slow = head;
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }
}// class

 

 

 

-

------------------------------------------------------------------------------------------------------------

 

package java_basic;

//--------------------------test---------------------------------
public class java_链表_2 {

    public static void main(String[] args) {
        LinkList linkList = new LinkList();
        linkList.addFirstNode("A");// 作为第一个,放上去
        linkList.addFirstNode("B");
        linkList.addFirstNode("C");
        linkList.add(1, "D");
        linkList.add(2, "E");
        linkList.add(3, "F");
        linkList.addLastNode("Z1");
        linkList.displayAllNodes();
        
        //
        Node node = linkList.deleteByData("A");
        System.out.println("node : " + node.data);
        linkList.displayAllNodes();
        Node node1 = linkList.findByPos(0);
        System.out.println("node1: " + node1.data);
        Node node2 = linkList.findByData("E");
        System.out.println("node2: " + node2.data);
    }
}// class

// ---------------定义链表的节点类--------------

class Node {
    Node next; // 下一节点
    String data;// 数据

    public Node(String data) {
        this.data = data;
    }

    // 显示此节点
    public void show() {
        System.out.print(data + "、");
    }
}

// ----------------- 定义链表类及其操作方法-----------------

class LinkList {
    public Node first; // 定义头结点
    private int pos = 0;// 节点的位置

    public LinkList() {
        this.first = null;
    }

    // ws:
    public void addLastNode(String string) {
        Node newNode = new Node(string);
        if (first == null) {
            first = newNode;
        } else {
            Node node=first;
            while(node.next!=null){
                node=node.next;
            }
            node.next=newNode;
        }
    }

    
    // 插入一个头节点
    public void addFirstNode(String data) {
        Node node = new Node(data);
        node.next = first;
        first = node;
    }

    // 删除一个头结点,并返回头结点
    public Node deleteFirstNode() {
        Node tempNode = first;
        first = tempNode.next;
        return tempNode;
    }

    // 在任意位置插入节点 在index的后面插入
    public void add(int index, String data) {
        Node node = new Node(data);
        Node current = first;
        Node previous = first;
        while (pos != index) {
            previous = current;
            current = current.next;
            pos++;
        }
        node.next = current;
        previous.next = node;
        pos = 0;
    }

    // 删除任意位置的节点
    public Node deleteByPos(int index) {
        Node current = first;
        Node previous = first;
        while (pos != index) {
            pos++;
            previous = current;
            current = current.next;
        }
        if (current == first) {
            first = first.next;
        } else {
            pos = 0;
            previous.next = current.next;
        }
        return current;
    }

    // 根据节点的data删除节点(仅仅删除第一个)
    public Node deleteByData(String data) {
        Node current = first;
        Node previous = first; // 记住上一个节点
        while (current.data != data) {
            if (current.next == null) {
                return null;
            }
            previous = current;
            current = current.next;
        }
        if (current == first) {
            first = first.next;
        } else {
            previous.next = current.next;
        }
        return current;
    }

    // 显示出所有的节点信息
    public void displayAllNodes() {
        Node current = first;
        while (current != null) {
            current.show();
            current = current.next;
        }
        System.out.println();
    }

    // 根据位置查找节点信息
    public Node findByPos(int index) {
        Node current = first;
        if (pos != index) {
            current = current.next;
            pos++;
        }
        return current;
    }

    // 根据数据查找节点信息
    public Node findByData(String data) {
        Node current = first;
        while (current.data != data) {
            if (current.next == null)
                return null;
            current = current.next;
        }
        return current;
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值