1. 程式人生 > >常用5個連結串列操作程式碼

常用5個連結串列操作程式碼

 * 1) 單鏈表反轉
 * 2) 連結串列中環的檢測
 * 3) 兩個有序的連結串列合併
 * 4) 刪除連結串列倒數第n個結點
 * 5) 求連結串列的中間結點
package algo.lesson8.LinkedList;

/**
 * 1) 單鏈表反轉
 * 2) 連結串列中環的檢測
 * 3) 兩個有序的連結串列合併
 * 4) 刪除連結串列倒數第n個結點
 * 5) 求連結串列的中間結點
 *
 * Author: Zheng
 */
public class LinkedListAlgo {
		
	public static void main(String[] args) {
		Node headNode = createNode(10);
		headNode.data = 0;
		Node p = headNode;
		for (int i = 1; i < 10; i++) { 
			Node node = createNode(i);
			node.data = i;
			node.next = null;
			p.next = node;
			p = node;
		}
		/*Node pNode = reverse(headNode);
		while(pNode != null){ 
			System.out.println(pNode.data); 
			pNode = pNode.next; 
		}
		
		boolean checkCircle = checkCircle(headNode);
		System.out.println("checkCircle----"+checkCircle); 
		Node findMiddleNode = findMiddleNode(headNode);

		System.out.println("findMiddleNode----"+findMiddleNode); 
		*/
		Node lastKth = deleteLastKth(headNode,5);
		while(lastKth != null){ 
			System.out.println(lastKth.data); 
			lastKth = lastKth.next; 
		}
	}

  // 單鏈表反轉
  public static Node reverse(Node list) {
	Node currentNode = list;
    Node headNode = null;
    Node previousNode = null;
    while (currentNode != null) {
      Node nextNode = currentNode.next;
      if (nextNode == null) {
        headNode = currentNode;
      }
      currentNode.next = previousNode;
      previousNode = currentNode;
      currentNode = nextNode;
    }

    return headNode;
  }

  // 檢測環
  public static boolean checkCircle(Node list) {
    if (list == null) return false;

    Node fast = list.next;
    Node slow = list;

    while (fast != null && fast.next != null) {
      fast = fast.next.next;
      slow = slow.next;

      if (slow == fast) return true;
    }

    return false;
  }

  // 有序連結串列合併
  public static Node mergeSortedLists(Node la, Node lb) {
    if (la == null) return lb;
    if (lb == null) return la;

    Node p = la;
    Node q = lb;
    Node head;
    if (p.data < q.data) {
      head = p;
      p = p.next;
    } else {
      head = q;
      q = q.next;
    }
    Node r = head;

    while (p != null && q != null) {
      if (p.data < q.data) {
        r.next = p;
        p = p.next;
      } else {
        r.next = q;
        q = q.next;
      }
      r = r.next;
    }

    if (p != null) {
      r.next = p;
    } else {
      r.next = q;
    }

    return head;
  }

  // 刪除倒數第K個結點
  public static Node deleteLastKth(Node list, int k) {
    Node fast = list;
    int i = 1;
    while (fast != null && i < k) {
      fast = fast.next;
      ++i;
    }

    if (fast == null) return list;

    Node slow = list;
    Node prev = null;
    while (fast.next != null) {
      fast = fast.next;
      prev = slow;
      slow = slow.next;
    }

    if (prev == null) {
      list = list.next;
    } else {
      prev.next = prev.next.next;
    }
    return list;
  }

  // 求中間結點
  public static Node findMiddleNode(Node list) {
    if (list == null) return null;

    Node fast = list;
    Node slow = list;

    while (fast.next != null && fast.next.next != null) {
      fast = fast.next.next;
      slow = slow.next;
    }

    return slow;
  }

  public static void printAll(Node list) {
    Node p = list;
    while (p != null) {
      System.out.print(p.data + " ");
      p = p.next;
    }
    System.out.println();
  }

  public static Node createNode(int value) {
    return new Node(value, null);
  }

  public static class Node {
    private int data;
    private Node next;

    public Node(int data, Node next) {
      this.data = data;
      this.next = next;
    }

    public int getData() {
      return data;
    }
  }

}