예제 #1
0
  public static void main(String[] args) {
    ListNode head = new ListNode(1);
    ListNode print = head, tail = head;
    ListNode n1 = new ListNode(2);
    tail.next = n1;
    tail = tail.next;
    ListNode n2 = new ListNode(3);
    tail.next = n2;
    tail = tail.next;
    ListNode n3 = new ListNode(4);
    tail.next = n3;
    tail = tail.next;
    ListNode n4 = new ListNode(5);
    tail.next = n4;
    tail = tail.next;

    System.out.print("The original list is: ");
    ListNode.print(print);
    System.out.print("The reversed list is: ");
    ListNode.print(reverseList(head));
  }
예제 #2
0
  // Test case
  public static void main(String[] args) {
    Solution02 solu = new Solution02();
    ListNode list = new ListNode(0);

    int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    Node head1 = list.creat(arr1);

    System.out.println("Linked List:");
    list.print(head1);
    Node k = solu.getKth(head1, 5);
    System.out.println("The 5th to last element is " + k.val);

    k = solu.getKth(head1, 0);
    System.out.println("The 0th to last element is " + k.val);
  }
  public static void main(String[] args) {
    ListNode l1 = new ListNode(1);
    ListNode l3 = new ListNode(2);
    ListNode l4 = new ListNode(5);
    l1.next = l3;
    l3.next = l4;

    ListNode l2 = new ListNode(1);
    ListNode l5 = new ListNode(2);
    ListNode l6 = new ListNode(5);
    l2.next = l5;
    l5.next = l6;

    AddTwoNumbers a = new AddTwoNumbers();
    ListNode result = a.addTwoNumbers1(l1, l2);
    result.print();
  }