- 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
public void display(Node newHead){
Node cur = newHead;
while(cur != null){
System.out.print(cur.getData()+" ");
cur = cur.getNext();
}
System.out.println();
}
public Node deleteDuplication(){
Node node = new Node(-1);
Node tmp = node;
Node cur = this.head;
while (cur != null){
if ((cur.getNext() != null) && (cur.getData() == cur.getNext().getData())){
while ((cur.getNext() != null) && (cur.getData() == cur.getNext().getData())){
cur = cur.getNext();
}
cur = cur.getNext();
}else{
tmp.setNext(cur);
tmp = cur;
cur = cur.getNext();
}
}
tmp.setNext(null);
return node.getNext();
}
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addLast(1);
myLinkedList.addLast(2);
myLinkedList.addLast(3);
myLinkedList.addLast(3);
myLinkedList.addLast(4);
myLinkedList.addLast(4);
myLinkedList.addLast(5);
Node newHead = myLinkedList.deleteDuplication();
myLinkedList.display(newHead);
}
}
- 链表的回文结构对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。所谓回文结构就是指形如:1,2,2,1又或是1,2,3,2,1这样的“对称”的链表!
public boolean chkPalindrome(){
Node slow = this.head;
Node fast = this.head;
while(fast != null && fast.getNext() != null){
fast = fast.getNext().getNext();
slow = slow.getNext();
}
Node cur = slow.getNext();
while(cur != null){
Node curNext = cur.getNext();
cur .setNext(slow);
slow = cur;
cur = curNext;
}
while(this.head != slow){
if (this.head.getData() != slow.getData()){
return false;
}
if (this.head.getNext() == slow){
return true;
}
this.head = this.head.getNext();
slow = slow.getNext();
}
return true;
}
public class TestDemo {
public static void createMeet(Node headA,Node headB){
headA.getNext().setNext(headB.getNext().getNext());
}
public static Node getIntersectionNode(Node headA,Node headB) {
if (headA == null || headB == null) {
return null;
}
Node pL = headA;
Node pS = headB;
int lA = 0;
int lB = 0;
while(pL != null){
lA++;
pL = pL.getNext();
}
while(pS != null){
lB++;
pS = pS.getNext();
}
pL = headA;
pS = headB;
int len = lA - lB;
if (len < 0){
pL = headB;
pS = headA;
len = lA - lB;
}
while(len > 0){
pL = pL.getNext();
len--;
}
while (pL != pS){
pL = pL.getNext();
pS = pS.getNext();
}
if (pL != null && pS != null && pS == pL){
return pL;
}
return null;
}
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addLast(10);
myLinkedList.addLast(30);
myLinkedList.addLast(50);
myLinkedList.addLast(70);
myLinkedList.display();
MyLinkedList myLinkedList1 = new MyLinkedList();
myLinkedList1.addLast(20);
myLinkedList1.addLast(40);
myLinkedList1.addLast(60);
myLinkedList1.addLast(80);
myLinkedList1.addLast(100);
myLinkedList1.display();
System.out.println("===========");
createMeet(myLinkedList.head,myLinkedList1.head);
Node ret = getIntersectionNode(myLinkedList.head,myLinkedList1.head);
System.out.println(ret.getData());
}
}
public void createLoop(){
Node cur = this.head;
while(cur.getNext() != null){
cur = cur.getNext();
}
cur.setNext(this.head.getNext().getNext());
}
public boolean hasCycle(){
Node fast = this.head;
Node slow = this.head;
while ((fast != null) && (fast.getNext() != null)){
fast = fast.getNext().getNext();
slow = slow.getNext();
if (fast == slow){
break;
}
}
if (fast == null || fast.getNext() == null){
return false;
}
return true;
}