Link: https://leetcode.com/problems/remove-linked-list-elements/
Given the head
of a linked list and an integer val
, remove all the nodes of the linked list that has Node.val == val
, and return the new head
.
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode dummyHead = new ListNode(-1, head);
ListNode previous = dummyHead;
ListNode current = head;
while (current != null) {
if (current.val == val){
current = current.next;
previous.next = current;
}
else {
previous = current;
current = current.next;
}
}
return dummyHead.next;
}
}
Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
A node in a singly linked list should have two attributes: val
and next
. val is the value of the current node, and next
is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev
to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.
Implement the MyLinkedList
class:
MyLinkedList()
Initializes the MyLinkedList object.int get(int index)
Get the value of the indexth node in the linked list. If the index is invalid, return -1
.void addAtHead(int val)
Add a node of value val
before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.void addAtTail(int val)
Append a node of value val
as the last element of the linked list.void addAtIndex(int index, int val)
Add a node of value val
before the indexth node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted.void deleteAtIndex(int index)
Delete the indexth node in the linked list, if the index is valid.class Node {
int val;
Node next;
public Node() {
}
public Node (int val) {
this.val = val;
}
}
class MyLinkedList {
Node dummyHead;
int size;
public MyLinkedList() {
size = 0;
dummyHead = new Node(-1);
}
public int get(int index) {
System.out.println(size);
if (index >= size)
return -1;
Node current = dummyHead;
for (int i = 0; i <= index; i++)
current = current.next;
return current.val;
}
public void addAtHead(int val) {
Node newHead = new Node(val);
newHead.next = dummyHead.next;
dummyHead.next = newHead;
size++;
}
public void addAtTail(int val) {
Node current = dummyHead;
while(current.next != null)
current = current.next;
current.next = new Node(val);
size++;
}
public void addAtIndex(int index, int val) {
if (index > size)
return;
Node current = dummyHead;
for (int i = 0; i < index; i++)
current = current.next;
Node next = current.next;
Node newNode = new Node(val);
newNode.next = next;
current.next = newNode;
size++;
}
public void deleteAtIndex(int index) {
if (index >= size)
return;
Node current = dummyHead;
for (int i = 0; i < index; i++)
current = current.next;
if (current.next != null)
current.next = current.next.next;
else
current.next = null;
size--;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/
Given the head
of a singly linked list, reverse the list, and return the reversed list.
head == null
or head.next == null
, return head
.pre = null
, cur = head
. While cur != null
:
cur.next
in temp
.cur.next = pre
.pre = cur
, cur = temp
.class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode pre = null;
ListNode cur = head;
ListNode next;
while(cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
//Recursive
class Solution {
public ListNode reverseList(ListNode head) {
return reverse(null, head);
}
private ListNode reverse(ListNode pre, ListNode cur) {
if (cur == null)
return pre;
ListNode temp = cur.next;
cur.next = pre;
return reverse(cur, temp);
}
}
//Recursive from last pointer
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null)
return head;
if (head.next == null)
return head;
ListNode last = reverseList(head.next);
head.next.next = head;
head.next = null;
return last;
}
}
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null)
return head;
if (head.next == null)
return head;
Stack<ListNode> stack = new Stack<>();
ListNode cur = head;
while(cur != null) {
stack.push(cur);
cur = cur.next;
}
ListNode dummyHead = new ListNode(-1);
cur = dummyHead;
while (!stack.isEmpty()) {
cur.next = stack.pop();
cur = cur.next;
}
cur.next = null;
return dummyHead.next;
}
}
Reference: https://programmercarl.com/