您的位置:首页 > 理论基础 > 数据结构算法

LinkedList各种操作

2016-01-23 18:36 447 查看
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.判断是否为回文

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息