LinkedList各種操作


public boolean isPalindrome(ListNode head){
		if(head == null){
			return true;
		}


		ListNode mid = findMiddle(head);
		ListNode sec = reverse(mid.next);


		while(head != null && sec != null && head.val == sec.val){
			head = head.next;
			sec = sec.next;
		}
		return sec == null;
	}

1、LinkedListノード構造
ListNode
public class ListNode{
	int val;
	ListNode next;
	ListNode(int x){
		val = x;
		next = null;
	}
	
}

2、中間ノードの検索方法
private ListNode findMiddle(ListNode head) {
        ListNode slow = head, fast = head.next;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

3.merge連結操作
public ListNode merge(ListNode head1,ListNode head2){
		ListNode dummy = new ListNode(0);
		ListNode ite = dummy;

		while(head1 != null && head2 != null){
			if(head1.val < head2.val){
				ite.next = head1;
				head1 = head1.next;
			}else{
				ite.next = head2.val;
				head2 = head2.next;
			}
			ite = ite.next;
		}
		if(head1 != null){
			ite.next = head1;
		}else{
			ite.next = head2;
		}
		return dummy.next;
	}

4、LiskedListの並べ替え方法1
public ListNode sortList(ListNode head){
		if(head == null || head.next == null){
			return head;
		}
		ListNode mid = findMiddle(head);
		ListNode right = sortList(mid.next);
		mid.next = null;
		ListNode left = sortList(head);
		return merge(left,right);
	}

5、末尾ノードを見つける
public ListNode getTail(ListNode head){
		if(head == null || head.next == null){
			return head;
		}
		while (head.next != null){
			head = head.next;
		}
		return head;
	}
6.3本のチェーンテーブルを接続する
public ListNode concat(ListNode left,ListNode middle,ListNode right){
		ListNode dummy = new ListNode(0),tail = dummy;
		tail.next = left;tail = getTail(tail);
		tail.next = middle;tail = getTail(tail);
		tail.next = right;tail = getTail(tail);
		return dummy.next;
	}

7、LinkedList並べ替え方法2快速並べ替え
	public ListNode sortList_quicksort(ListNode head){
		if(head == null || head.next == null){
			return head;
		}
		ListNode mid = findMiddle(head);

		ListNode leftDummy = new ListNode(0),leftTail = leftDummy;
		ListNode middleDummy = new ListNode(0),middleTail = middleDummy;
		ListNode rightDummy = new ListNode(0), rightTail = rightDummy;

		while (head != null){
			if(head.val < mid.val){
				leftTail.next = head;
				leftTail = leftTail.next;
			}else if(head.val > mid.val){
				rightTail.next = head;
				rightTail = rightTail.next;
			}else{
				middleTail.next = head;
				middleTail = middleTail.next;
			}
			head = head.next;
		}

		leftTail.next = null;
		rightTail.next = null;
		middleTail.next = null;
		ListNode left = sortList_quicksort(leftDummy.next);
		ListNode right = sortList_quicksort(rightDummy.next);

		return concat(left, middleDummy.next,right);


	}

8、チェーンテーブルの反転
private ListNode reverse(ListNode head) {
        ListNode newHead = null;
        while (head != null) {
            ListNode temp = head.next;
            head.next = newHead;
            newHead = head;
            head = temp;
        }
        return newHead;
    }

9.LingkedListにループがあるかどうかを判断する
public boolean hasCycle(ListNode head){
		HashSet<ListNode> set = new HashSet<ListNode>();
		
		while(head != null){
			if(set.contains(head)){
				return true;
			}else{
				set.add(head);
			}
			head = head.next;
		}
		return false;
	}

10.指定されたノードを削除
public void deleteNode(ListNode node) {  
        //input check  
        if(node==null) return;  
        node.val = node.next.val;  
        node.next = node.next.next;  
    } 

11.返信であるか否かを判断する