예제 #1
0
 public Integer pop() {
   if (top.next == null) {
     return null;
   }
   int val = top.next.data;
   top.next = top.next.next;
   return val;
 }
  public static LinkNode stackImplementation(LinkNode linkedList) {
    Stack<LinkNode> stack = new Stack<>();
    LinkNode tempNode = linkedList;
    while (tempNode != null) {
      stack.push(tempNode);
      tempNode = tempNode.next;
    }
    tempNode = stack.pop();
    linkedList = tempNode;
    while (!stack.empty()) {

      tempNode.next = stack.pop();
      tempNode = tempNode.next;
    }
    tempNode.next = null;

    return linkedList;
  }
  public static void main(String args[]) {
    LinkNode linkNode = new LinkNode(10);

    linkNode.next = new LinkNode(20);
    linkNode.next.next = new LinkNode(30);
    linkNode.next.next.next = new LinkNode(40);

    LinkNode reversedLinkedList = stackImplementation(linkNode);
    System.out.println("Done");
  }
예제 #4
0
 public void push(Integer data) {
   LinkNode node = new LinkNode(data);
   node.next = top.next;
   top.next = node;
 }
예제 #5
0
 Stack() {
   top = new LinkNode(-1);
   top.next = null;
 }