从尾到头打印链表
题目描述:https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
解法1:链表插头法
/**
* 头插法
* @param head
* @return
*/
public int[] reversePrint(ListNode listNode) {
ListNode head = new ListNode(-1);
while(listNode != null) {
ListNode memo = listNode.next;
listNode.next = head.next;
head.next = listNode;
listNode = memo;
}
List<Integer> list = new ArrayList<Integer>();
head = head.next;
while(head != null) {
list.add(head.val);
head = head.next;
}
int[] res = new int[list.size()];
for(int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
解法2:借助辅助栈
/**
* 辅助栈法
* @param listNode
* @return
*/
public int[] reversePrint2(ListNode listNode) {
LinkedList<ListNode> stack = new LinkedList<ListNode>();
while(listNode != null) {
stack.add(listNode);
listNode = listNode.next;
}
List<ListNode> list = new ArrayList<ListNode>();
while(!stack.isEmpty()) {
list.add(stack.removeLast());
}
int[] res = new int[list.size()];
for(int i = 0; i < list.size(); i++) {
res[i] = list.get(i).val;
}
return res;
}
删除链表的节点
题目描述:https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val == val) {
head = head.next;
return head;
}
ListNode pointer = head;
while(pointer.next != null) {
if(pointer.next.val == val) {
pointer.next = pointer.next.next;
break;
}
pointer = pointer.next;
}
return head;
}
}
删除链表重复节点
解法1:使用set,去除重复
/**
* 借用set来去重复
* @param pHead
* @return
*/
public ListNode deleteDuplication(ListNode pHead) {
if(pHead == null) return null;
ListNode pre = pHead;
ListNode cur = pHead.next;
HashSet<Integer> set = new HashSet<Integer>();
while(cur != null) {
if(pre.val == cur.val) {
set.add(cur.val);
}
pre = cur;
cur = cur.next;
}
while(set.contains(pHead.val)) {
pHead = pHead.next;
}
if(pHead == null) return null;
pre = pHead;
cur = pHead.next;
while(cur != null) {
if(set.contains(cur.val)) {
pre.next = cur.next;
cur = cur.next;
}else {
pre = cur;
cur = cur.next;
}
}
return pHead;
}
解法2:构造辅助头节点
/**
* 构造新的头节点,来避免对头节点进行额外讨论
* @param pHead
* @return
*/
public ListNode deleteDuplication2(ListNode pHead) {
ListNode head = new ListNode(Integer.MIN_VALUE);
head.next = pHead;
ListNode pre = head;
ListNode cur = head.next;
while(cur != null) {
if(cur.next != null && cur.val == cur.next.val) {
while(cur.next != null && cur.val == cur.next.val) {
cur = cur.next;
}
cur = cur.next;
pre.next = cur;
}else {
pre = cur;
cur = cur.next;
}
}
return head.next;
}
链表中倒数第k个节点
题目描述:https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/
解法1:利用辅助栈,逆序这样的问题都应该想到这个办法
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
if(head == null) return null;
Stack<ListNode> stack = new Stack<ListNode>();
ListNode cur = head;
stack.add(head);
while(cur.next != null) {
stack.add(cur.next);
cur = cur.next;
}
ListNode retNode = null;
for(int i = 0; i < k; i++) {
if(stack.isEmpty()) return null;
retNode = stack.pop();
}
return retNode;
}
}
链表中环的入口节点
解法:借助Hashmap进行重复发现
class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
if(pHead == null) return null;
HashMap<ListNode, Integer> map = new HashMap<ListNode, Integer>();
ListNode pointer = pHead;
int value = 0;
map.put(pHead, value = map.containsKey(pHead) ? 1 : 0);
while(pointer.next != null) {
if(map.containsValue(1)) return pointer;
System.out.println(map.containsKey(pointer.next));
map.put(pointer.next, value = map.containsKey(pointer.next) ? 1 : 0);
pointer = pointer.next;
}
return null;
}
}
反转链表
解法1:辅助栈法
/**
* 辅助栈法
* @param head
* @return
*/
public ListNode reverseList(ListNode head) {
if(head == null) return null;
if(head.next == null) return head;
ListNode pre = head;
ListNode cur = head.next;
Stack<ListNode> stack = new Stack<ListNode>();
stack.add(pre);
stack.add(cur);
while(cur.next != null) {
stack.add(cur.next);
pre = cur;
cur = cur.next;
}
head = stack.pop();
head.next = null;
cur = head;
ListNode temp;
while(!stack.isEmpty()) {
temp= stack.pop();
temp.next = null;
cur.next = temp;
cur = temp;
}
return head;
}
解法2:迭代法
public ListNode reverseList2(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while(cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}