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.连接三条链表

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.判断是否为回文


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值