1.反转一个单链表。
public class MySingleLinkedlmpl {
class Node{
public int getData() {
return data;
}
private int data;
public Node next;
public Node(int data){
this.data=data;
this.next=null;
}
}//节点类
private Node head;
public MySingleLinkedlmpl(){
this.head=null;
}
public Node reverseList(){
Node reverseHead=null;//反转后的新头节点
Node prev=null;//cur的前驱
Node cur=this.head;//cur代表的是当前需要反转的节点
while (cur!=null){
Node curNext=cur.next;
if(curNext==null){
reverseHead=cur;
}
cur.next=prev;
prev=cur;
cur=curNext;
}
return reverseHead;
}
}
2.给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
public class MySingleLinkedlmpl {
class Node{
public int getData() {
return data;
}
private int data;
public Node next;
public Node(int data){
this.data=data;
this.next=null;
}
}//节点类
private Node head;
public MySingleLinkedlmpl(){
this.head=null;
}
public Node detectCycle(){
Node fast=this.head;
Node slow=this.head;
while (fast!=null && fast.next.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow ){
break;
}
}
//没有环
if(fast==null || fast.next.next==null){
return null;
}
//将slow拉到头,然后和fast一人一步走
slow=this.head;
while (fast!=slow){
fast=fast.next;
slow=slow.next;
}
return slow;
}
}
3.判断链表是否有环。
public class MySingleLinkedlmpl {
class Node{
public int getData() {
return data;
}
private int data;
public Node next;
public Node(int data){
this.data=data;
this.next=null;
}
}//节点类
private Node head;
public MySingleLinkedlmpl(){
this.head=null;
}
public boolean hasCycle(){
Node fast=this.head;
Node slow=this.head;
while (fast!=null && fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow){
return true;
}
}
return false;
}
}
4.链表的回文结构。
public class MySingleLinkedlmpl {
class Node{
public int getData() {
return data;
}
private int data;
public Node next;
public Node(int data){
this.data=data;
this.next=null;
}
}//节点类
private Node head;
public MySingleLinkedlmpl(){
this.head=null;
}
public boolean chkPalindrome(){
Node fast=this.head;
Node slow=this.head;
while (fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
}
//slow指向的位置就是中间位置
Node p=slow.next;
Node pNext=p.next;
while (p!=null){
p.next=slow;
slow=p;
p=p.next;
if(p!=null){
pNext=p.next;
}
}
//后半部分已经进行反转
while (this.head!=slow){
if(this.head.data!=slow.data){
return false;
}
if(this.head.next==slow){
return true;
}
head=head.next;
slow=slow.next;
}
return true;
}
}
5.在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头
指针。
public class MySingleLinkedlmpl {
class Node{
public int getData() {
return data;
}
private int data;
public Node next;
public Node(int data){
this.data=data;
this.next=null;
}
}//节点类
private Node head;
public MySingleLinkedlmpl(){
this.head=null;
}
public Node deleteDuplication(){
Node node=new Node(-1);
Node cur=this.head;
Node tmpHead=node;
while (cur!=null){
if(cur.next!=null&&cur.data==cur.next.data){
while (cur.next!=null&&cur.data==cur.next.data){
cur=cur.next;
}
cur=cur.next;
//cur指向的这个节点和前面的节点不同
tmpHead.next=cur;
}else{
//确定不为重复的节点,串
tmpHead.next=cur;
tmpHead=cur;
cur=cur.next;
}
}
return node.next;
}
}