package offer.linklist; public class LinkList { // 頭結點 public Node head; // 當前結點 public Node current; // 添加結點 public void add(int data) { // 判斷鏈表是否爲null if (head == null) { // 鏈表爲null,則表示鏈表中尚未結點 // 頭結點爲新加入結點 head = new Node(data); // 當前的結點變爲頭結點 current = head; } else { // 鏈表不爲null,則當前結點的下一個結點爲新加入結點 current.next = new Node(data); // 當前的結點變爲新加入的結點 current = current.next;// 這一步是關鍵,別忘!!!! } } }
package offer.linklist; public class Node { public int data; public Node next; public Node(int data) { this.data = data; } @Override public String toString() { // TODO Auto-generated method stub return String.valueOf(data); } }
package offer.linklist; public class Test { // 遍歷鏈表 public static void printLinkList(LinkList list) { if (list != null) { Node current = list.head; while (current != null) { System.out.print("--->" + current.data); current = current.next; } } } // 鏈表的結點數 public static int NodeNum(LinkList list) { if (list == null || list.head == null) { return 0; } int count = 0; Node current = list.head; while (current != null) { count++; current = current.next; } return count; } // 查找鏈表中第k個結點 public static Node findNode(LinkList list, int k) { if (list == null || list.head == null) { return null; } Node current = list.head; int count = 1; while (current != null) { if (count == k) { return current; } count++; current = current.next; } return null; } // 查找鏈表中倒數第k個結點(方法一) public static Node findLastNode1(LinkList list, int k) { if (list == null || list.head == null) { return null; } int nodeNum = NodeNum(list); if (k < nodeNum) { return findNode(list, nodeNum - k + 1); } return null; } // 查找鏈表中倒數第k個結點(方法二) public static Node findLastNode2(LinkList list, int k) { if (list == null || list.head == null) { return null; } // 第一個標記 Node frist = list.head; // 第二個標記 Node second = list.head; int count = 0; // 將第二個標記移動到k個結點 while (count != k && second != null) { count++; second = second.next; } if (count != k) { return null; } // 將第一個和第二個標記日後移動,直到第二個標記達到最後一個結點,則第一個標記的結點就是咱們要找的結點 while (second != null && frist != null) { frist = frist.next; second = second.next; } return frist; } // 查找鏈表的中間結點 public static Node findMidNode(LinkList list) { if (list == null || list.head == null) { return null; } Node frist = list.head; Node second = list.head; // frist 走一步 second 走兩步 直到second走到最後一個結點 while (frist != null && second != null && second.next != null) { frist = frist.next; second = second.next.next; } return frist; } // 合併兩個有序鏈表,合併後仍然有序 public static LinkList mergeOrderedLinkList(LinkList list1, LinkList list2) { if (list1 == null || list1.head == null) { return list2; } else if (list2 == null || list2.head == null) { return list1; } LinkList list = new LinkList(); if (list1.head.data > list2.head.data) { list.head = list2.head; list2.head = list2.head.next; } else { list.head = list1.head; list1.head = list1.head.next; } // 當前結點 Node current = list.head; while (current != null && list1.head != null && list2.head != null) { if (list1.head.data < list2.head.data) { current.next = list1.head; list1.head = list1.head.next; } else { current.next = list2.head; list2.head = list2.head.next; } current = current.next; } if (list1.head != null) { current.next = list1.head; } if (list2.head != null) { current.next = list2.head; } return list; } // 反轉鏈表 面試出現率最高 public static LinkList reverseLinkList(LinkList list) { if (list == null || list.head == null || list.head.next == null) { return list; } Node current = list.head;// 舊鏈表的頭結點 Node next;// 用來保存下一下結點 LinkList linkList = new LinkList(); while (current != null) { next = current.next; current.next = linkList.head;// 關鍵代碼 linkList.head = current;// 關鍵代碼 current = next; } return linkList; } public static void main(String[] args) { LinkList list01 = new LinkList(); for (int i = 0; i < 5; i++) { list01.add(i); } printLinkList(reverseLinkList(list01)); // System.out.println(NodeNum(list01)); // System.out.println("================="); // printLinkList(list01); // System.out.println(); // System.out.println("================="); // System.out.println(findNode(list01, 2)); // System.out.println("================="); // System.out.println(findLastNode1(list01, 2)); // System.out.println("================="); // System.out.println(findLastNode2(list01, 2)); // System.out.println(findLastNode2(list01, 8)); // System.out.println("================="); // System.out.println(findMidNode(list01)); // System.out.println("================="); // LinkList list02 = new LinkList(); // for (int i = 0; i < 10; i = i + 2) { // list02.add(i); // } // printLinkList(list02); // System.out.println(); // System.out.println("================="); // printLinkList(mergeOrderedLinkList(list01, list02));// // printLinkList(mergeOrderedLinkList(list01, // // null)); // System.out.println(); // System.out.println("================="); } }