/*
*@program: algStudy
*@description: 链表节点
*@author: chensy
*@create: 2019-07-12 20:59
*/
class ListNode
{
public int data;
public ListNode next;
public ListNode(int data){this.data = data;}
/*
* @Author chensy
* @Description //TODO 删除最后一个节点
* @Date 23:17 2019/7/12
* @Param [first]
* @return ListNode
**/
public static ListNode deleteLastNode(ListNode head) {
if(head == null || head.next == null)
{
return head;
}
ListNode tmpNode = new ListNode(-1);
tmpNode.next = head;
while(head.next.next != null)
{
head = head.next;
}
head.next = null;
return tmpNode.next;
}
/*
* @Author chensy
* @Description //TODO 删除指定索引节点, 索引从0开始
* @Date 23:17 2019/7/12
* @Param [node, index]
* @return ListNode
**/
public static ListNode delete(ListNode head, int index) {
if(head == null)
{
return null;
}
ListNode tmpNode = new ListNode(-1);
tmpNode.next = head;
int count = 1;
while (head.next != null)
{
if(count++ == index)
{
head.next = head.next.next;
}
//假若删除项在最后一项
if(head.next == null)
{
break;
}
head = head.next;
}
return tmpNode.next;
}
/*
* @Author chensy
* @Description //TODO 查找item是否存在该链表
* @Date 23:18 2019/7/12
* @Param [head, item]
* @return boolean
**/
public static boolean find(ListNode head, int item) {
if(head == null || (head.next == null && head.data != item))
{
return false;
}
while ( head.next != null)
{
if(head.data == item)
{
return true;
}
head = head.next;
}
return false;
}
/*
* @Author chensy
* @Description //TODO 删除node后续节点
* @Date 23:18 2019/7/12
* @Param [head, node]
* @return ListNode
**/
public static ListNode removeAfter(ListNode head, ListNode node) {
if(node == null || head == null || head.data == node.data)
{
return null;
}
ListNode tmpNode = new ListNode(-1);
tmpNode.next = head;
while (head.next != null)
{
if(head.data == node.data)
{
head.next = null;
break;
}
head = head.next;
}
return tmpNode.next;
}
/*
* @Author chensy
* @Description //TODO 合并节点, 右合并到左
* @Date 23:18 2019/7/12
* @Param [left, right]
* @return ListNode
**/
public static ListNode insertNode(ListNode left, ListNode right) {
if(left == null && right == null)
{
return null;
}
if (left == null) {
return right;
}
if(right == null)
{
return left;
}
ListNode tmpNode = new ListNode(-1);
tmpNode.next = left;
left.next = right;
return tmpNode.next;
}
/*
* @Author chensy
* @Description //TODO 删除指定Key的所有节点
* @Date 23:19 2019/7/12
* @Param [node, key]
* @return ListNode
**/
public static ListNode remove(ListNode node, int key) {
if(node == null || (node.next == null && node.data == key))
{
return null;
}
ListNode tmpNode = new ListNode(-1);
tmpNode.next = node;
while(node != null && node.next != null)
{
if(node.next.data == key) {
node.next = node.next.next;
}
//防止删除的是倒数第二个
if (node.next != null && node.next.data == key)
{
node.next = null;
break;
}
node = node.next;
}
return tmpNode.next;
}
/*
* @Author chensy
* @Description //TODO 查找链表中的最大值, 若不存在返回0 (循环解法)
* @Date 13:26 2019/7/13
* @Param [head]
* @return int
**/
public static int max (ListNode head) {
if(head == null)
{
return 0;
}
int max = head.data;
while (head != null)
{
if(head.data > max)
{
max = head.data;
}
head = head.next;
}
return max;
}
/*
* @Author chensy
* @Description //TODO 查找链表中的最大值, 若不存在返回0 (递归解法)
* @Date 13:33 2019/7/13
* @Param [head]
* @return int
**/
public static int max2(ListNode head) {
if(head == null)
{
return 0;
}
return head.next == null ? head.data : max(head.next);
}
}